/** * 向任意 worker 进程或者 task 进程发送消息 * * 和 swoole 不同的是, 它支持服务器集群下向任意集群去投递数据 * * @param $data * @param int $workerId * @param int $serverId * @param string $serverGroup * @return bool */ public function sendMessage($data, $workerId, $serverId = -1, $serverGroup = null) { if ($serverId < 0 || Server::$clustersType === 0 || $this->serverId === $serverId && null === $serverGroup) { # 没有指定服务器ID 或者 本服务器 或 非集群模式 if ($this->name !== 'Main') { $obj = new \stdClass(); $obj->_sys = true; $obj->name = $this->name; $obj->sid = Server::$serverId; $obj->data = $data; $data = $obj; } return $this->server->sendMessage($data, $workerId); } else { $client = Clusters\Client::getClient($serverGroup, $serverId, $workerId, true); if (!$client) { return false; } return $client->sendData('msg', $data, $this->name); } }
/** * 向任意 worker 进程或者 task 进程发送消息 * * 和 swoole 不同的是, 它支持服务器集群下向任意集群去投递数据 * * @param $data * @param int $workerId * @param int $serverId * @return bool */ public function sendMessage($data, $workerId, $serverId = -1) { if ($serverId === -1) { return $this->server->sendMessage($data, $workerId); } else { $client = Clusters\Client::getClient($serverId, $workerId, true); if (!$client) { return false; } return $client->sendData('msg', $data, 'Task'); } }