client.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. require __DIR__.'/../generated/vendor/autoload.php';
  3. require_once __DIR__ . '/../vendor/autoload.php';
  4. use App\WebSocketClient;
  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 MsgID {
  11. const ENTERHALL = 1;
  12. const RESENTERHALL = 2;
  13. }
  14. class GameClient {
  15. private ?WebSocketClient $client = null;
  16. private ?string $userId = null;
  17. private ?string $nikeName = null;
  18. public function start() {
  19. $this->login();
  20. }
  21. private function login() {
  22. $loginReq = new ReqLogin();
  23. $loginReq->setAccount("xy1");
  24. $loginReq->setPassword('123');
  25. $binaryData = $loginReq->serializeToString();
  26. if (empty($binaryData)) {
  27. throw new Exception("序列化失败:返回空数据");
  28. }
  29. $options = [
  30. 'http' => [
  31. 'method' => 'POST',
  32. 'header' => implode("\r\n", [
  33. 'Content-Type: application/x-protobuf',
  34. 'Accept: */*',
  35. 'Accept-Language: zh-CN,zh;q=0.9',
  36. 'Connection: keep-alive',
  37. 'Referer: http://localhost:7456/'
  38. ]),
  39. 'content' => $binaryData,
  40. 'timeout' => 10,
  41. 'ignore_errors' => false
  42. ]
  43. ];
  44. try {
  45. $url = 'http://120.76.130.29:8080/ReqLogin';
  46. $context = stream_context_create($options);
  47. $response = file_get_contents($url, false, $context);
  48. if ($response === false) {
  49. throw new Exception("请求失败: " . error_get_last()['message']);
  50. }
  51. if (strpos($response, '404 page not found') !== false) {
  52. throw new Exception("API端点不存在(404),请检查URL: " . $url);
  53. }
  54. $userResponse = new ResLogin();
  55. $userResponse->mergeFromString($response);
  56. if ($userResponse->getErrMsg() !== null) {
  57. throw new Exception("登录失败: " . $userResponse->getErrMsg()->getMessage());
  58. }
  59. echo "登录成功!\n";
  60. echo "用户ID: " . $userResponse->getUserId() . "\n";
  61. echo "昵称: " . $userResponse->getNikeName() . "\n";
  62. $this->userId = $userResponse->getUserId();
  63. $this->nikeName = $userResponse->getNikeName();
  64. $this->initWebSocketConnection();
  65. echo "是否成功获取信息: " .$userResponse->hasUserInfo() . "\n";
  66. if ($userResponse->hasUserInfo()) {
  67. $this->enterHall($userResponse->getUserInfo()->getUserId());
  68. }
  69. } catch (Exception $e) {
  70. echo $e->getMessage() . "\n";
  71. exit(1);
  72. }
  73. }
  74. private function initWebSocketConnection() {
  75. $this->client = new WebSocketClient('ws://localhost:3653');
  76. $this->client->on(MsgID::RESENTERHALL, function($binaryData) {
  77. $response = new ResEnterHall();
  78. $response->mergeFromString($binaryData);
  79. echo "成功进入大厅!\n";
  80. echo "大厅玩家数: " . $response->getSuccess() . "\n";
  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. $binaryData = $enterHall->serializeToString();
  91. $this->client->sendBinary(MsgID::ENTERHALL, $binaryData);
  92. echo "已发送进入大厅请求\n";
  93. }
  94. }
  95. // 启动客户端
  96. $gameClient = new GameClient();
  97. $gameClient->start();