123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- namespace App; // 添加命名空间
- require __DIR__.'/../generated/vendor/autoload.php';
- require_once __DIR__ . '/../vendor/autoload.php';
- require_once './msg.php';
- use App\WebSocketClient;
- use Common_pack\EnterHall;
- use Common_pack\ReqLogin;
- use Common_pack\ResEnterHall;
- use Common_pack\ResHeartBeat;
- use Common_pack\ResLogin;
- use Exception;
- use Google\Protobuf\Internal\Message;
- use MsgID;
- class GameClient {
- private ?WebSocketClient $client = null;
- private ?string $userId = null;
- private ?string $nikeName = null;
- private ?int $listenerPid = null;
- private bool $isRunning = false;
- public function start() {
- // $this->login();
- }
- public function close() {
- echo "命令调用了 close \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";
- });
- }
- 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();
|