ChannelService.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Channel 进程通信服务
  4. // +----------------------------------------------------------------------
  5. namespace app\services\workerman;
  6. use Channel\Client;
  7. class ChannelService
  8. {
  9. protected $trigger = 'muyinjie';
  10. protected static $instance;
  11. public function __construct()
  12. {
  13. self::connet();
  14. }
  15. public static function instance()
  16. {
  17. if (is_null(self::$instance)) {
  18. self::$instance = new self();
  19. }
  20. return self::$instance;
  21. }
  22. public static function connet()
  23. {
  24. $config = config('workerman.channel');
  25. Client::connect($config['ip'], $config['port']);
  26. }
  27. public function setTrigger(string $name)
  28. {
  29. $this->trigger = $name;
  30. return $this;
  31. }
  32. public function send(string $type, ?array $data = null, ?array $ids = null)
  33. {
  34. $res = compact('type');
  35. if (!is_null($data)) $res['data'] = $data;
  36. if (!is_null($ids) && count($ids)) $res['ids'] = $ids;
  37. $this->trigger($this->trigger, $res);
  38. $this->trigger = 'muyinjie';
  39. }
  40. public function trigger(string $type, ?array $data = null)
  41. {
  42. Client::publish($type, $data);
  43. }
  44. }