client.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. require __DIR__.'/../generated/vendor/autoload.php';
  3. require_once __DIR__ . '/../vendor/autoload.php';
  4. use App\BinaryWebSocketClient;
  5. use Common_pack\EnterHall;
  6. use Common_pack\ReqLogin;
  7. use Common_pack\ResEnterHall;
  8. use Common_pack\ResLogin;
  9. use Google\Protobuf\Internal\Message;
  10. class GameClient {
  11. private ?BinaryWebSocketClient $client = null;
  12. private ?string $userId = null;
  13. private ?string $nikeName = null;
  14. public function start() {
  15. $this->login();
  16. }
  17. private function login() {
  18. $loginReq = new ReqLogin();
  19. $loginReq->setAccount("xy1");
  20. $loginReq->setPassword('123');
  21. $binaryData = $loginReq->serializeToString();
  22. if (empty($binaryData)) {
  23. throw new Exception("序列化失败:返回空数据");
  24. }
  25. $options = [
  26. 'http' => [
  27. 'method' => 'POST',
  28. 'header' => implode("\r\n", [
  29. 'Content-Type: application/x-protobuf',
  30. 'Accept: */*',
  31. 'Accept-Language: zh-CN,zh;q=0.9',
  32. 'Connection: keep-alive',
  33. 'Referer: http://localhost:7456/'
  34. ]),
  35. 'content' => "\n\x03xy1\x12\x03123", // 与JS相同的二进制数据
  36. 'timeout' => 10,
  37. 'ignore_errors' => false
  38. ]
  39. ];
  40. $context = stream_context_create($options);
  41. try {
  42. $url = 'http://120.76.130.29:8080/ReqLogin';
  43. $context = stream_context_create($options);
  44. $response = file_get_contents($url, false, $context);
  45. if (strpos($response, '404 page not found') !== false) {
  46. throw new Exception("API端点不存在(404),请检查URL: " . $url);
  47. }
  48. $userResponse = new ResLogin();
  49. if ($response !== false) {
  50. $userResponse->mergeFromString($response);
  51. print_r($userResponse->getNikeName());
  52. if ($userResponse->getErrMsg()!=null) {
  53. throw new Exception("登录失败: ".$userResponse->getErrMsg()->getMessage());
  54. }
  55. echo "登录成功!\n";
  56. echo "用户ID: ".$userResponse->getUserId()."\n";
  57. echo "昵称: ".$userResponse->getNikeName()."\n";
  58. $this->userId = $userResponse->getUserId();
  59. $this->nikeName = $userResponse->getNikeName();
  60. // 登录成功后才建立WebSocket连接
  61. $this->initWebSocketConnection();
  62. if ($userResponse->hasUserInfo()) {
  63. $this->enterHall($userResponse->getUserInfo()->getUserId());
  64. }
  65. } else {
  66. throw new Exception("请求失败: ".error_get_last()['message']);
  67. }
  68. } catch (Exception $e) {
  69. echo $e->getMessage()."\n";
  70. exit(1);
  71. }
  72. }
  73. private function initWebSocketConnection() {
  74. // 初始化WebSocket客户端
  75. $this->client = new BinaryWebSocketClient('https://leaf.ktle.top');
  76. // 注册消息处理器
  77. $this->client->on(MsgID::RESENTERHALL, function(ResEnterHall $response) {
  78. echo "成功进入大厅!\n";
  79. echo "大厅玩家数: ".$response->getSuccess()."\n";
  80. });
  81. // 开始监听消息
  82. $this->client->startListening();
  83. }
  84. private function enterHall($userId) {
  85. if (!$this->client) {
  86. throw new Exception("WebSocket连接未建立");
  87. }
  88. $enterHall = new EnterHall();
  89. $enterHall->setUserId($userId);
  90. $this->client->sendBinary(MsgID::ENTERHALL, $enterHall);
  91. echo "已发送进入大厅请求\n";
  92. }
  93. }
  94. // 启动客户端
  95. $gameClient = new GameClient();
  96. $gameClient->start();