Events.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\push\controller;
  12. use GatewayWorker\Lib\Gateway;
  13. use think\Hook;
  14. use Workerman\Lib\Timer;
  15. class Events
  16. {
  17. //定时器间隔
  18. protected static $interval = 2;
  19. //定时器
  20. protected static $timer = null;
  21. /**
  22. *
  23. * @var object
  24. */
  25. protected static $worker;
  26. /**
  27. * 事件处理类
  28. * @var string
  29. */
  30. protected static $evevtRunClass = \app\push\controller\EvevtRun::class;
  31. /**
  32. * 消息事件回调
  33. * @var string
  34. */
  35. protected static $eventClassName = \app\push\controller\Push::class;
  36. /**
  37. * 当客户端发来消息时触发
  38. * @param int $client_id 连接id
  39. * @param mixed $message 具体消息
  40. */
  41. public static function onMessage($client_id, $message)
  42. {
  43. $message_data = json_decode($message, true);
  44. if (!$message_data) return;
  45. try {
  46. if (!isset($message_data['type'])) throw new \Exception('缺少消息参数类型');
  47. //消息回調处理
  48. $evevtName = self::$eventClassName . '::instance';
  49. if (is_callable($evevtName))
  50. $evevtName()->start($message_data['type'], $client_id, $message_data);
  51. else
  52. throw new \Exception('消息处理回调不存在。[' + $evevtName + ']');
  53. } catch (\Exception $e) {
  54. var_dump([
  55. 'file' => $e->getFile(),
  56. 'code' => $e->getCode(),
  57. 'msg' => $e->getMessage(),
  58. 'line' => $e->getLine()
  59. ]);
  60. }
  61. }
  62. /**
  63. * 当用户连接时触发的方法
  64. * @param integer $client_id 连接的客户端
  65. * @return void
  66. */
  67. public static function onConnect($client_id)
  68. {
  69. Gateway::sendToClient($client_id, json_encode(array(
  70. 'type' => 'init',
  71. 'client_id' => $client_id
  72. )));
  73. }
  74. /**
  75. * 当用户断开连接时触发的方法
  76. * @param integer $client_id 断开连接的客户端
  77. * @return void
  78. */
  79. public static function onClose($client_id)
  80. {
  81. Gateway::sendToClient($client_id, json_encode([
  82. 'type' => 'logout',
  83. 'message' => "client[$client_id]"
  84. ]));
  85. }
  86. /**
  87. * 当进程启动时
  88. * @param integer $businessWorker 进程实例
  89. */
  90. public static function onWorkerStart($worker)
  91. {
  92. //在进程1上开启定时器 每self::$interval秒执行
  93. self::$worker = $worker;
  94. if ($worker->id === 0) {
  95. $last = time();
  96. $task = [6 => $last, 10 => $last, 30 => $last, 60 => $last, 180 => $last, 300 => $last];
  97. self::$timer = Timer::add(self::$interval, function () use (&$task) {
  98. try {
  99. $now = time();
  100. Hook::exec(self::$evevtRunClass);
  101. foreach ($task as $sec => &$time) {
  102. if (($now - $time) >= $sec) {
  103. $time = $now;
  104. Hook::exec(self::$evevtRunClass, 'task_' . $sec);
  105. }
  106. }
  107. } catch (\Throwable $e) {
  108. }
  109. });
  110. }
  111. }
  112. /**
  113. * 当进程关闭时
  114. * @param integer $businessWorker 进程实例
  115. */
  116. public static function onWorkerStop($worker)
  117. {
  118. if ($worker->id === 0) Timer::del(self::$timer);
  119. }
  120. }