protected function parsePacket($str, &$sessionId, &$serverInfo) { if ($arr = json_decode($str, true)) { $sessionId = $serverInfo = null; if (isset($arr[$this->sessionKey]) && $arr[$this->sessionKey]) { $sessionId = $arr[$this->sessionKey]; } if ($packet = Packet::getPacketFromArray($arr)) { return $packet; } } return false; }
}); $client->on("connect", function (swoole_client $cli) { // send a auth packet $packet = new AuthPacket(); $packet->setFrom(0); $packet->setTo('system'); $packet->setAccount(G::$account); $packet->setPassword(G::$password); $cli->send(serializePacket($packet)); }); $client->on("receive", function (swoole_client $cli, $message) { //echo 'receive: ',$message,"\n"; if (($data = substr($message, 4)) && ($result = ConvertUtil::unpack($data)) && isset($result['from']) && isset($result['to']) && isset($result['type']) && isset($result['content'])) { G::$sessionId = isset($result['session_id']) ? $result['session_id'] : null; /* @var Packet $packet */ $packet = Packet::getPacketFromArray($result); if (!G::$auth) { $authPacket = new AuthPacket(); $authPacket->setFrom(G::$account); $authPacket->setTo('system'); if ($packet->getType() == $authPacket->getType()) { $authPacket->setContent($packet->getContent()); switch ($authPacket->getStatus()) { case AuthPacket::STATUS_NEED_AUTH: $authPacket->setAccount(G::$account); $authPacket->setPassword(G::$password); $cli->send(serializePacket($authPacket)); break; case AuthPacket::STATUS_AUTH_FAILED: echo "error: auth failed\n"; break;
protected function parsePacket($str, &$sessionId, &$serverInfo) { $sessionId = $serverInfo = null; if ($arr = ConvertUtil::unpack($str)) { if (isset($arr[$this->serverKey])) { if (!($serverInfo = $this->getRegister($arr[$this->serverKey]))) { return false; } } if (isset($arr[$this->sessionKey]) && $arr[$this->sessionKey]) { $sessionId = $arr[$this->sessionKey]; } if ($packet = Packet::getPacketFromArray($arr)) { return $packet; } } return false; }
protected function dealMessage($data) { echo "begin to deal message\n"; if ($data && ($result = ConvertUtil::unpack($data)) && isset($result['from']) && isset($result['to']) && isset($result['type']) && isset($result['content'])) { $this->sessionId = isset($result['session_id']) ? $result['session_id'] : null; /* @var Packet $packet * @var AuthPacket $authPacket */ $packet = Packet::getPacketFromArray($result); if (!$this->auth) { if ($authPacket = AuthPacket::getPacketFromPacket($packet)) { switch ($authPacket->getStatus()) { case AuthPacket::STATUS_NEED_AUTH: echo "[ERROR] need auth\n"; break; case AuthPacket::STATUS_AUTH_FAILED: echo "[ERROR] auth failed\n"; break; case AuthPacket::STATUS_AUTH_SUCCESS: echo "[SUCCESS] auth success\n"; $this->auth = true; $this->userId = $authPacket->getUid(); break; default: echo "[ERROR] auth status undefined!\n"; } } else { echo "[ERROR] first packet is not auth packet\n"; } } echo "---------RECEIVE----------------------------\n"; echo "from: ", $packet->getFrom(), "\n"; echo "to: ", $packet->getTo(), "\n"; echo "type: ", $packet->getType(), "\n"; echo "session: ", $this->sessionId, "\n"; echo "content: ", json_encode($packet->getContent(), JSON_UNESCAPED_UNICODE), "\n\n"; } else { echo "[ERROR] receive error\n"; } }