| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
- // +----------------------------------------------------------------------
- // | Channel 进程通信服务
- // +----------------------------------------------------------------------
- namespace app\services\workerman;
- use Channel\Client;
- class ChannelService
- {
- protected $trigger = 'muyinjie';
- protected static $instance;
- public function __construct()
- {
- self::connet();
- }
- public static function instance()
- {
- if (is_null(self::$instance)) {
- self::$instance = new self();
- }
- return self::$instance;
- }
- public static function connet()
- {
- $config = config('workerman.channel');
- Client::connect($config['ip'], $config['port']);
- }
- public function setTrigger(string $name)
- {
- $this->trigger = $name;
- return $this;
- }
- public function send(string $type, ?array $data = null, ?array $ids = null)
- {
- $res = compact('type');
- if (!is_null($data)) $res['data'] = $data;
- if (!is_null($ids) && count($ids)) $res['ids'] = $ids;
- $this->trigger($this->trigger, $res);
- $this->trigger = 'muyinjie';
- }
- public function trigger(string $type, ?array $data = null)
- {
- Client::publish($type, $data);
- }
- }
|