Events.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. use \GatewayWorker\Lib\Gateway;
  21. use Workerman\MySQL\Connection;
  22. /**
  23. * 主逻辑
  24. * 主要是处理 onConnect onMessage onClose 三个方法
  25. * onConnect 和 onClose 如果不需要可以不用实现并删除
  26. */
  27. class Events
  28. {
  29. protected static $db;
  30. protected static $prefix;
  31. /**
  32. * 进行启动时操作
  33. *
  34. * @param mixed $businessWorker
  35. * @return void
  36. */
  37. public static function onWorkerStart($businessWorker)
  38. {
  39. // 数据库连接
  40. $config = [
  41. // 连接地址
  42. 'host' => 'localhost',
  43. // 端口
  44. 'port' => '3306',
  45. // 用户名
  46. 'user' => 'niushop',
  47. // 密码
  48. 'passwd' => 'niushop',
  49. // 表前缀
  50. 'prefix' => '',
  51. // 数据库名称
  52. 'dbname' => 'niushop',
  53. ];
  54. self::$prefix = $config['prefix'];
  55. self::$db = new Connection($config['host'], $config['port'], $config['user'], $config['passwd'], $config['dbname']);
  56. }
  57. /**
  58. * 当客户端连接时触发
  59. * 如果业务不需此回调可以删除onConnect
  60. * @param int $client_id 连接id
  61. */
  62. public static function onConnect($client_id)
  63. {
  64. $message = json_encode(['type' => 'init', 'data' => ['client_id' => $client_id]]);
  65. // 向当前client_id发送数据
  66. Gateway::sendToClient($client_id, $message);
  67. }
  68. /**
  69. * 当客户端发来消息时触发
  70. * @param int $client_id 连接id
  71. * @param mixed $message 具体消息
  72. */
  73. public static function onMessage($client_id, $message)
  74. {
  75. // 向所有人发送
  76. // Gateway::sendToAll("$client_id said $message\r\n");
  77. }
  78. /**
  79. * 当用户断开连接时触发
  80. * @param int $client_id 连接id
  81. */
  82. public static function onClose($client_id)
  83. {
  84. if (isset($_SESSION['servicer_id'])) {
  85. // 客服离线,连接断开
  86. @self::$db->update(self::$prefix . 'servicer')->cols(['online' => 0, 'client_id' => ''])
  87. ->where("client_id = '$client_id' ")->query();
  88. $servicer_id = $_SESSION['servicer_id'];
  89. @self::$db->update(self::$prefix . 'servicer_member')->cols(['online' => 0, 'client_id' => ''])
  90. ->where("servicer_id = $servicer_id")->query();
  91. } else {
  92. // 用户离线,连接断开
  93. @self::$db->update(self::$prefix . 'servicer_member')->cols(['online' => 0, 'client_id' => ''])
  94. ->where("client_id = '$client_id' ")->query();
  95. }
  96. }
  97. }