Gateway::getOnlineStatus PHP Метод

getOnlineStatus() публичный статический Метод

获取在线状态,目前返回一个在线client_id数组
public static getOnlineStatus ( ) : array
Результат array
    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;
    }

Usage Example

Пример #1
0
 public static function deleteCode($companyId, $code)
 {
     Gateway::getOnlineStatus();
     $store = Store::instance('wymenu');
     $ret = $store->delete($companyId . $code);
     /*if(Yii::app()->params->has_cache)
       {
           $ccode = apc_delete($companyId.$code);                    
       }*/
 }
All Usage Examples Of Gateway::getOnlineStatus