protected static function sendUdpAndRecv($address , $data)
{
$buffer = GatewayProtocol::encode($data);
// 非workerman环境,使用udp发送数据
$client = stream_socket_client("udp://$address", $errno, $errmsg);
if(strlen($buffer) == stream_socket_sendto($client, $buffer))
{
// 阻塞读
stream_set_blocking($client, 1);
// 1秒超时
stream_set_timeout($client, 1);
// 读udp数据
$data = fread($client, 655350);
// 返回结果
return json_decode($data, true);
}
else
{
throw new \Exception("sendUdpAndRecv($address, \$bufer) fail ! Can not send UDP data!", 502);
}
}
判断客户端的状态,给客户端对应的gateway发送udp,但是怎么找到客服端对应的连接呢
udp数据里面包含了客户端标示client_id,gateway收到udp数据解析得到client_id,从当前进程中可以直接得到对应的连接,因为所有连接都存储在一个数组中,数组的key就是client_id
$this->storeClientAddress($connection->globalClientId, $address);
case GatewayProtocol::CMD_IS_ONLINE:
$connection->send((int)isset($this->_clientConnections[$data['client_id']]));