ChannelService.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace crmeb\services\workerman;
  3. use Channel\Client;
  4. class ChannelService
  5. {
  6. /**
  7. * @var Client
  8. */
  9. protected $channel;
  10. /**
  11. * @var ChannelService
  12. */
  13. protected static $instance;
  14. public function __construct()
  15. {
  16. self::connet();
  17. }
  18. public static function instance()
  19. {
  20. if (is_null(self::$instance))
  21. self::$instance = new self();
  22. return self::$instance;
  23. }
  24. public static function connet()
  25. {
  26. $config = config('workerman.channel');
  27. Client::connect($config['ip'], $config['port']);
  28. }
  29. /**
  30. * 发送消息
  31. * @param string $type 类型
  32. * @param array|null $data 数据
  33. * @param array|null $ids 用户 id,不传为全部用户
  34. */
  35. public function send(string $type, ? array $data = null, ?array $ids = null)
  36. {
  37. $res = compact('type');
  38. if (!is_null($data))
  39. $res['data'] = $data;
  40. if (!is_null($ids) && count($ids))
  41. $res['ids'] = $ids;
  42. $this->trigger('crmeb', $res);
  43. }
  44. public function trigger(string $type, ?array $data = null)
  45. {
  46. Client::publish($type, $data);
  47. }
  48. }