client.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. require __DIR__.'/../generated/vendor/autoload.php';
  3. require __DIR__.'/WebSocketClient.php';
  4. use Common_pack\EnterHall;
  5. use Common_pack\ReqLogin;
  6. use Common_pack\ResEnterHall;
  7. use Common_pack\ResLogin;
  8. class GameClient {
  9. private ?BinaryWebSocketClient $client = null;
  10. private ?string $userId = null;
  11. private ?string $nikeName = null;
  12. public function start() {
  13. $this->login();
  14. }
  15. private function login() {
  16. $loginReq = new ReqLogin();
  17. $loginReq->setAccount("xy1");
  18. $loginReq->setPassword('123');
  19. $binaryData = $loginReq->serializeToString();
  20. if (empty($binaryData)) {
  21. throw new Exception("序列化失败:返回空数据");
  22. }
  23. $options = [
  24. 'http' => [
  25. 'header' => [
  26. 'Content-Type: application/x-protobuf',
  27. 'X-Protobuf-Schema: common.proto',
  28. 'X-Protobuf-Message: common_pack.ReqLogin'
  29. ],
  30. 'method' => 'POST',
  31. 'content' => $binaryData,
  32. 'ignore_errors' => true,
  33. 'timeout' => 10
  34. ]
  35. ];
  36. $context = stream_context_create($options);
  37. try {
  38. $response = file_get_contents('http://120.76.130.29:8080/RegLogin', false, $context);
  39. if ($response !== false) {
  40. $userResponse = new ResLogin();
  41. $userResponse->mergeFromString($response);
  42. if ($userResponse->hasErrMsg()) {
  43. throw new Exception("登录失败: ".$userResponse->getErrMsg()->getMessage());
  44. }
  45. echo "登录成功!\n";
  46. echo "用户ID: ".$userResponse->getUserId()."\n";
  47. echo "昵称: ".$userResponse->getNikeName()."\n";
  48. $this->userId = $userResponse->getUserId();
  49. $this->nikeName = $userResponse->getNikeName();
  50. // 登录成功后才建立WebSocket连接
  51. $this->initWebSocketConnection();
  52. if ($userResponse->hasUserInfo()) {
  53. $this->enterHall($userResponse->getUserInfo()->getUserId());
  54. }
  55. } else {
  56. throw new Exception("请求失败: ".error_get_last()['message']);
  57. }
  58. } catch (Exception $e) {
  59. echo $e->getMessage()."\n";
  60. exit(1);
  61. }
  62. }
  63. private function initWebSocketConnection() {
  64. // 初始化WebSocket客户端
  65. $this->client = new BinaryWebSocketClient('ws://game.example.com:8080');
  66. // 注册消息处理器
  67. $this->client->on(MsgID::RESENTERHALL, function(ResEnterHall $response) {
  68. echo "成功进入大厅!\n";
  69. echo "大厅玩家数: ".$response->getSuccess()."\n";
  70. });
  71. // 开始监听消息
  72. $this->client->startListening();
  73. }
  74. private function enterHall($userId) {
  75. if (!$this->client) {
  76. throw new Exception("WebSocket连接未建立");
  77. }
  78. $enterHall = new EnterHall();
  79. $enterHall->setUserId($userId);
  80. $this->client->sendBinary(MsgID::ENTERHALL, $enterHall);
  81. echo "已发送进入大厅请求\n";
  82. }
  83. }
  84. // 启动客户端
  85. $gameClient = new GameClient();
  86. $gameClient->start();