Events.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. /**
  15. * 用于检测业务代码死循环或者长时间阻塞等问题
  16. * 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
  17. * 然后观察一段时间workerman.log看是否有process_timeout异常
  18. */
  19. //declare(ticks=1);
  20. /**
  21. * 聊天主逻辑
  22. * 主要是处理 onMessage onClose
  23. */
  24. //use \GatewayWorker\Lib\Gateway;
  25. //use \GatewayWorker\Lib\DbConnection;
  26. use think\Db;
  27. use think\App;
  28. class Events
  29. {
  30. public static function onWorkerStart(){
  31. App::initCommon();
  32. /*$info = Db::name('mt_admin')->find(1);
  33. dump($info);*/
  34. }
  35. /**
  36. * 有消息时
  37. * @param int $client_id
  38. * @param mixed $message
  39. */
  40. public static function onMessage($client_id, $message)
  41. {
  42. dump($message);
  43. return ;
  44. }
  45. /**
  46. * 当客户端断开连接时
  47. * @param integer $client_id 客户端id
  48. */
  49. public static function onClose($client_id)
  50. {
  51. // debug
  52. /* echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:$client_id onClose:''\n";
  53. // 从房间的客户端列表中删除
  54. if(isset($_SESSION['room_id']))
  55. {
  56. $room_id = $_SESSION['room_id'];
  57. $new_message = array('type'=>'logout', 'from_client_id'=>$client_id, 'from_client_name'=>$_SESSION['client_name'], 'time'=>date('Y-m-d H:i:s'));
  58. Gateway::sendToGroup($room_id, json_encode($new_message));
  59. }*/
  60. }
  61. }