ChatService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace crmeb\services\workerman\chat;
  3. use Channel\Client;
  4. use Channel\Server;
  5. use crmeb\services\workerman\ChannelService;
  6. use crmeb\services\workerman\Response;
  7. use Workerman\Connection\TcpConnection;
  8. use Workerman\Lib\Timer;
  9. use Workerman\Worker;
  10. class ChatService
  11. {
  12. /**
  13. * @var Worker
  14. */
  15. protected $worker;
  16. /**
  17. * @var TcpConnection[]
  18. */
  19. protected $connections = [];
  20. /**
  21. * @var TcpConnection[]
  22. */
  23. protected $user = [];
  24. /**
  25. * @var ChatHandle
  26. */
  27. protected $handle;
  28. /**
  29. * @var Response
  30. */
  31. protected $response;
  32. /**
  33. * @var int
  34. */
  35. protected $timer;
  36. public function __construct(Worker $worker)
  37. {
  38. $this->worker = $worker;
  39. $this->handle = new ChatHandle($this);
  40. $this->response = new Response();
  41. }
  42. public function setUser(TcpConnection $connection)
  43. {
  44. $this->user[$connection->user->uid] = $connection;
  45. }
  46. public function user()
  47. {
  48. return $this->user;
  49. }
  50. public function onConnect(TcpConnection $connection)
  51. {
  52. $this->connections[$connection->id] = $connection;
  53. $connection->lastMessageTime = time();
  54. }
  55. public function onMessage(TcpConnection $connection, $res)
  56. {
  57. $connection->lastMessageTime = time();
  58. $res = json_decode($res, true);
  59. if (!$res || !isset($res['type']) || !$res['type'] || $res['type'] == 'ping') return;
  60. if (!method_exists($this->handle, $res['type'])) return;
  61. $this->handle->{$res['type']}($connection, $res + ['data' => []], $this->response->connection($connection));
  62. }
  63. public function onWorkerStart(Worker $worker)
  64. {
  65. ChannelService::connet();
  66. Client::on('crmeb_chat', function ($eventData) use ($worker) {
  67. if (!isset($eventData['type']) || !$eventData['type']) return;
  68. $ids = isset($eventData['ids']) && count($eventData['ids']) ? $eventData['ids'] : array_keys($this->user);
  69. foreach ($ids as $id) {
  70. if (isset($this->user[$id]))
  71. $this->response->connection($this->user[$id])->success($eventData['type'], $eventData['data'] ?? null);
  72. }
  73. });
  74. $this->timer = Timer::add(15, function () use (&$worker) {
  75. $time_now = time();
  76. foreach ($worker->connections as $connection) {
  77. if ($time_now - $connection->lastMessageTime > 12) {
  78. $this->response->connection($connection)->close('timeout');
  79. }
  80. }
  81. });
  82. }
  83. public function onClose(TcpConnection $connection)
  84. {
  85. var_dump('close');
  86. unset($this->connections[$connection->id]);
  87. if (isset($connection->user->uid)) {
  88. unset($this->user[$connection->user->uid]);
  89. }
  90. }
  91. }