public static function close() { if (!is_null(self::$socket)) { foreach (self::$socket as $s) { socket_close($s); } self::$socket = null; } }
/** * 后端进程推送消息 * @param $param * @return bool */ public function receive($param) { if (empty($param)) { return false; } $param = json_decode($param, true); if (empty($param['data'])) { return false; } $client_ip = $param['client_ip']; if (isset($param['cmd0']) && isset($param['cmd1']) && isset($param['cmd2'])) { echo "\n===============[AsyncProtocol:cmd0={$cmd0}|cmd1={$cmd1}|cmd2={$cmd2}|player_id={$player_id}]==============\n"; $cmd0 = $param['cmd0']; $cmd1 = $param['cmd1']; $cmd2 = $param['cmd2']; $player_id = $param['player_id']; Protocol::make_data($param['data']); $protocol = Protocol::output($cmd0, $cmd1, $cmd2, 2, $player_id); } else { //直接推送协议 不用封装 $protocol = base64_decode($param['data']); } echo "\n===============[receive]==============\n"; var_dump($client_ip); if (DEBUG != 0) { $protocol_copy = $protocol; # 协议数据副本 $used_bytes = 0; # 已读取字节数 while ($remain_data = substr($protocol_copy, $used_bytes, 12)) { $used_bytes += 12; $head = unpack("Vlen/Vcmd/Vplayer_id", $remain_data); $cmd = $head['cmd']; $cmd0 = ($cmd & 0xff000000) >> 24; $cmd1 = ($cmd & 0xff0000) >> 16; $cmd2 = $cmd & 0xffff; $player_id = $head['player_id']; $rlen = $head['len'] - 12; $body_bin = substr($protocol_copy, $used_bytes, $rlen); # 实际协议体,msgpack_pack打包的二进制流 $used_bytes += $rlen; if (!empty($body_bin)) { $body_real = msgpack_unpack($body_bin); $json = json_encode($body_real); $date = date('Y-m-d H:i:s'); echo "\n===============[AsyncProtocol:date={$date}|cmd0={$cmd0}|cmd1={$cmd1}|cmd2={$cmd2}|player_id={$player_id}]==============\n"; echo "{$json}\n"; } else { } } } echo "\n===============[receive]==============\n"; // $clients = array('192.168.1.228','192.168.1.136','192.168.1.165','192.168.1.155','192.168.1.25','192.168.1.24','192.168.1.224','192.168.1.33','192.168.1.121'); #$clients = array(GATEWAY_IP,'192.168.1.24','192.168.1.33','192.168.1.136','192.168.1.92','192.168.1.224','192.168.1.41'); $res = Com_GatewayClient::send($protocol, GATEWAY_IP); // $clients = array(GATEWAY_IP); // foreach($clients as $client_ip){ // // // } // foreach($clients as $client_ip){ #Com_GatewayClient::send($protocol,$client_ip); //Com_GatewayClient::send($protocol,GATEWAY_IP); // if($client_ip == GATEWAY_IP){ // Com_GatewayClient::send($protocol,$client_ip); // } // if($client_ip != '192.168.1.41'){ // Com_GatewayClient::send($protocol,'192.168.1.41'); // } // // if($client_ip != '192.168.1.228'){ // Com_GatewayClient::send($protocol,'192.168.1.228'); // } // Com_GatewayClient::send($protocol,'192.168.1.165'); // Com_GatewayClient::send($protocol,'192.168.1.155'); // } // Com_GatewayClient::send($protocol,$client_ip); // Com_Curl::send($protocol,$client_ip); }