login(); } public function close() { echo "命令调用了 close \n"; } public function getFriends(){ $userResponse = new ReqFriendList(); $userResponse->setUserId($this->userId); $binaryData = $userResponse->serializeToString(); $this->client->sendBinary(MsgID::REQFRIENDLIST, $binaryData); echo $this->userId; echo "已发送获取好友列表请求\n"; } public function outGame(){ echo "命令调用了 outGame \n"; } public function login(string $account , string $password) { $loginReq = new ReqLogin(); $loginReq->setAccount($account); $loginReq->setPassword($password); $binaryData = $loginReq->serializeToString(); if (empty($binaryData)) { throw new Exception("序列化失败:返回空数据"); } $options = [ 'http' => [ 'method' => 'POST', 'header' => implode("\r\n", [ 'Content-Type: application/x-protobuf', 'Accept: */*', 'Accept-Language: zh-CN,zh;q=0.9', // 'Connection: keep-alive', 'Connection: close', 'Referer: http://localhost:7456/' ]), 'content' => $binaryData, 'timeout' => 10, 'ignore_errors' => false ] ]; try { echo "登陆请求时间:"; echo date('Y-m-d H:i:s'); echo "\n"; $url = 'http://120.76.130.29:8080/ReqLogin'; $context = stream_context_create($options); $response = file_get_contents($url, false, $context); if ($response === false) { throw new Exception("请求失败: " . error_get_last()['message']); } if (strpos($response, '404 page not found') !== false) { throw new Exception("API端点不存在(404),请检查URL: " . $url); } echo "登陆响应时间:"; echo date('Y-m-d H:i:s'); echo "\n"; $userResponse = new ResLogin(); $userResponse->mergeFromString($response); if ($userResponse->getErrMsg() !== null) { throw new Exception("登录失败: " . $userResponse->getErrMsg()->getMessage()); } echo "登录成功!\n"; echo "用户ID: " . $userResponse->getUserId() . "\n"; echo "昵称: " . $userResponse->getNikeName() . "\n"; $this->userId = $userResponse->getUserId(); $this->nikeName = $userResponse->getNikeName(); $this->initWebSocketConnection(); echo "是否成功获取信息: " .$userResponse->hasUserInfo() . "\n"; if ($userResponse->hasUserInfo()) { $this->enterHall($userResponse->getUserInfo()->getUserId()); $this->startListening(); } } catch (Exception $e) { echo $e->getMessage() . "\n"; exit(1); } } private function initWebSocketConnection() { // wss://leaf.ktle.top $this->client = new WebSocketClient('ws://localhost:3653'); $this->client->on(MsgID::RESENTERHALL, function($binaryData) { $response = new ResEnterHall(); $response->mergeFromString($binaryData); echo "成功进入大厅!\n"; }); $this->client->on(MsgID::RESHEARTBEAT, function($binaryData) { $response = new ResHeartBeat(); $response->mergeFromString($binaryData); // echo "心跳:".$response->getMsg(); // echo "\n"; }); $this->client->on(MsgID::RESFRIENDLIST, function($binaryData) { $response = new ResFriendList(); $response->mergeFromString($binaryData); if($response->getSuccess()){ var_dump($response->getList()); echo "\n"; }else{ echo "获取好友列表失败"; echo "\n"; } }); } private function startListening() { if (!$this->client) { throw new Exception("WebSocket连接未建立"); } if ($this->listenerPid !== null) { return; // 已经启动监听 } echo "开始监听WebSocket消息...\n"; $pid = pcntl_fork(); if ($pid == -1) { throw new Exception("无法创建子进程"); } elseif ($pid == 0) { // 子进程:持续监听WebSocket $this->isRunning = true; $this->client->startListening(); exit(0); } else { // 父进程记录子进程ID $this->listenerPid = $pid; $this->isRunning = true; } } public function isRunning(): bool { return $this->isRunning; } private function enterHall($userId) { if (!$this->client) { throw new Exception("WebSocket连接未建立"); } $enterHall = new EnterHall(); $enterHall->setUserId($userId); $binaryData = $enterHall->serializeToString(); $this->client->sendBinary(MsgID::ENTERHALL, $binaryData); echo "已发送进入大厅请求\n"; } } // 启动客户端 // $gameClient = new GameClient(); // $gameClient->start();