public static function getOnlineStatus()
{
$gateway_data = GatewayProtocol::$empty;
$gateway_data['cmd'] = GatewayProtocol::CMD_GET_ONLINE_STATUS;
$gateway_buffer = GatewayProtocol::encode($gateway_data);
$all_addresses = Store::instance('gateway')->get('GLOBAL_GATEWAY_ADDRESS');
$client_array = $status_data = array();
// 批量向所有gateway进程发送CMD_GET_ONLINE_STATUS命令
foreach ($all_addresses as $address) {
$client = stream_socket_client("udp://{$address}", $errno, $errmsg);
if (strlen($gateway_buffer) === stream_socket_sendto($client, $gateway_buffer)) {
$client_id = (int) $client;
$client_array[$client_id] = $client;
}
}
// 超时1秒
$time_out = 1;
$time_start = microtime(true);
// 批量接收请求
while (count($client_array) > 0) {
$write = $except = array();
$read = $client_array;
if (@stream_select($read, $write, $except, $time_out)) {
foreach ($read as $client) {
// udp
$data = json_decode(fread($client, 65535), true);
if ($data) {
$status_data = array_merge($status_data, $data);
}
unset($client_array[$client]);
}
}
if (microtime(true) - $time_start > $time_out) {
break;
}
}
return $status_data;
}