Events.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * 用于检测业务代码死循环或者长时间阻塞等问题
  4. * 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
  5. * 然后观察一段时间workerman.log看是否有process_timeout异常
  6. */
  7. //declare(ticks=1);
  8. namespace app\gateway;
  9. use \GatewayWorker\Lib\Gateway;
  10. use app\api\model\plus\chat\Chat as ChatModel;
  11. use Workerman\Lib\Timer;
  12. use Workerman\Worker;
  13. use think\worker\Application;
  14. use think\facade\Cache;
  15. use app\common\service\message\MessageService;
  16. /**
  17. * 主逻辑
  18. * 主要是处理 onConnect onMessage onClose 三个方法
  19. * onConnect 和 onClose 如果不需要可以不用实现并删除
  20. */
  21. class Events
  22. {
  23. /**
  24. * onWorkerStart 事件回调
  25. * 当businessWorker进程启动时触发。每个进程生命周期内都只会触发一次
  26. *
  27. * @access public
  28. * @param \Workerman\Worker $businessWorker
  29. * @return void
  30. */
  31. public static function onWorkerStart(Worker $businessWorker)
  32. {
  33. $app = new Application;
  34. $app->initialize();
  35. // 5秒执行一次定时任务
  36. Timer::add(5, function () use (&$task) {
  37. try {
  38. event('JobScheduler');
  39. } catch (\Throwable $e) {
  40. echo 'ERROR: ' . $e->getMessage() . PHP_EOL;
  41. }
  42. });
  43. }
  44. /**
  45. * 当客户端连接时触发
  46. * 如果业务不需此回调可以删除onConnect
  47. *
  48. * @param int $client_id 连接id
  49. */
  50. public static function onConnect($client_id)
  51. {
  52. // 向当前client_id发送数据
  53. $data['client_id'] = $client_id;
  54. $data['type'] = 'init';
  55. Gateway::sendToClient($client_id, json_encode($data));
  56. }
  57. /**
  58. * 当客户端发来消息时触发
  59. * @param int $client_id 连接id
  60. * @param mixed $message 具体消息
  61. */
  62. public static function onMessage($client_id, $message)
  63. {
  64. $data = json_decode($message, 1);
  65. $data['status'] = 0;
  66. $to = 0;
  67. $from_id = 0;
  68. if (isset($data['msg_type']) && $data['msg_type'] == 2) {
  69. $to = 'supplier_' . $data['supplier_user_id'];
  70. $from_id = $data['user_id'];
  71. } else {
  72. $to = $data['user_id'];
  73. $from_id = 'supplier_' . $data['supplier_user_id'];
  74. }
  75. if ($data['type'] !== 'ping' && $data['type'] !== 'close') {//正常发送消息
  76. if (Gateway::isUidOnline($to)) {
  77. $data['status'] = 1;
  78. $data['time'] = date('Y-m-d H:i:s');
  79. Gateway::sendToUid($to, json_encode($data));
  80. }
  81. $Chat = new ChatModel;
  82. $Chat->add($data);
  83. self::sendMessage($data);
  84. } else if ($data['type'] == 'ping') {
  85. //心跳
  86. $data['Online'] = $to && Gateway::isUidOnline($to) ? 'on' : 'off';
  87. Gateway::sendToUid($from_id, json_encode($data));
  88. } else if ($data['type'] == 'close') {
  89. //断开链接
  90. Gateway::unbindUid($client_id, $from_id);
  91. }
  92. }
  93. private static function sendMessage($data)
  94. {
  95. //给供应商发送未读消息
  96. if (isset($data['shop_supplier_id']) && $data['shop_supplier_id']) {
  97. //供应商缓存状态
  98. $status = Cache::get('message_' . $data['shop_supplier_id']);
  99. if (!$status) {
  100. //未读消息
  101. $count = (new ChatModel())->where('shop_supplier_id', '=', $data['shop_supplier_id'])
  102. ->where('status', '=', 0)
  103. ->where('msg_type', '=', 2)
  104. ->count();
  105. if ($count > 0) {
  106. Cache::set('message_' . $data['shop_supplier_id'], 1, 7200);
  107. // 发送模板消息
  108. $send['create_time'] = time();
  109. $send['send_user'] = $data['from_id'];
  110. $send['message'] = $data['content'] . ",您还有{$count}条消息未读";
  111. $send['user_id'] = $data['to'];
  112. (new MessageService)->supplierMsg($send);
  113. }
  114. }
  115. }
  116. }
  117. /**
  118. * 当用户断开连接时触发
  119. * @param int $client_id 连接id
  120. */
  121. public static function onClose($client_id)
  122. {
  123. // 向所有人发送
  124. //GateWay::sendToAll("$client_id logout\r\n");
  125. }
  126. }