protected static function sendUdpAndRecv($address , $data)
{
$buffer = GatewayProtocol::encode($data);
// 非workerman環(huán)境,使用udp發(fā)送數(shù)據(jù)
$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數(shù)據(jù)
$data = fread($client, 655350);
// 返回結(jié)果
return json_decode($data, true);
}
else
{
throw new \Exception("sendUdpAndRecv($address, \$bufer) fail ! Can not send UDP data!", 502);
}
}
判斷客戶端的狀態(tài),給客戶端對應(yīng)的gateway發(fā)送udp,但是怎么找到客服端對應(yīng)的連接呢