Example #1
0
 /**
  * 获取在线状态,目前返回一个在线client_id数组,client_id为key
  * @return array
  */
 public static function getAllClientInfo($group = null)
 {
     $gateway_data = GatewayProtocol::$empty;
     if (!$group) {
         $gateway_data['cmd'] = GatewayProtocol::CMD_GET_ALL_CLIENT_INFO;
     } else {
         $gateway_data['cmd'] = GatewayProtocol::CMD_GET_CLINET_INFO_BY_GROUP;
         $gateway_data['ext_data'] = $group;
     }
     $status_data = array();
     $all_buffer_array = self::getBufferFromAllGateway($gateway_data);
     foreach ($all_buffer_array as $local_ip => $buffer_array) {
         foreach ($buffer_array as $local_port => $buffer) {
             $data = json_decode(rtrim($buffer), true);
             if ($data) {
                 foreach ($data as $connection_id => $session_buffer) {
                     $client_id = Context::addressToClientId($local_ip, $local_port, $connection_id);
                     if ($client_id === Context::$client_id) {
                         $status_data[$client_id] = (array) $_SESSION;
                     } else {
                         $status_data[$client_id] = $session_buffer ? Context::sessionDecode($session_buffer) : array();
                     }
                 }
             }
         }
     }
     return $status_data;
 }
Example #2
0
 /**
  * 当gateway转发来数据时
  * @param TcpConnection $connection
  * @param mixed $data
  */
 public function onGatewayMessage($connection, $data)
 {
     $cmd = $data['cmd'];
     if ($cmd === GatewayProtocol::CMD_PING) {
         return;
     }
     // 上下文数据
     Context::$client_ip = $data['client_ip'];
     Context::$client_port = $data['client_port'];
     Context::$local_ip = $data['local_ip'];
     Context::$local_port = $data['local_port'];
     Context::$connection_id = $data['connection_id'];
     Context::$client_id = Context::addressToClientId($data['local_ip'], $data['local_port'], $data['connection_id']);
     // $_SERVER变量
     $_SERVER = array('REMOTE_ADDR' => long2ip($data['client_ip']), 'REMOTE_PORT' => $data['client_port'], 'GATEWAY_ADDR' => long2ip($data['local_ip']), 'GATEWAY_PORT' => $data['gateway_port'], 'GATEWAY_CLIENT_ID' => Context::$client_id);
     // 尝试解析session
     if ($data['ext_data'] != '') {
         $_SESSION = Context::sessionDecode($data['ext_data']);
     } else {
         $_SESSION = null;
     }
     // 备份一次$data['ext_data'],请求处理完毕后判断session是否和备份相等,不相等就更新session
     $session_str_copy = $data['ext_data'];
     if ($this->processTimeout) {
         pcntl_alarm($this->processTimeout);
     }
     // 尝试执行Event::onConnection、Event::onMessage、Event::onClose
     switch ($cmd) {
         case GatewayProtocol::CMD_ON_CONNECTION:
             if ($this->_eventOnConnect) {
                 call_user_func($this->_eventOnConnect, Context::$client_id);
             }
             break;
         case GatewayProtocol::CMD_ON_MESSAGE:
             if ($this->_eventOnMessage) {
                 call_user_func($this->_eventOnMessage, Context::$client_id, $data['body']);
             }
             break;
         case GatewayProtocol::CMD_ON_CLOSE:
             if ($this->_eventOnClose) {
                 call_user_func($this->_eventOnClose, Context::$client_id);
             }
             break;
     }
     if ($this->processTimeout) {
         pcntl_alarm(0);
     }
     // 判断session是否被更改
     $session_str_now = $_SESSION !== null ? Context::sessionEncode($_SESSION) : '';
     if ($session_str_copy != $session_str_now) {
         \Utils\GatewayWorker\Lib\Gateway::updateSocketSession(Context::$client_id, $session_str_now);
     }
     Context::clear();
 }