123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- require __DIR__.'/../generated/vendor/autoload.php';
- require_once __DIR__ . '/../vendor/autoload.php';
- use App\BinaryWebSocketClient;
- use Common_pack\EnterHall;
- use Common_pack\ReqLogin;
- use Common_pack\ResEnterHall;
- use Common_pack\ResLogin;
- use Google\Protobuf\Internal\Message;
- class GameClient {
- private ?BinaryWebSocketClient $client = null;
- private ?string $userId = null;
- private ?string $nikeName = null;
- public function start() {
- $this->login();
- }
- private function login() {
- $loginReq = new ReqLogin();
- $loginReq->setAccount("xy1");
- $loginReq->setPassword('123');
-
- $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',
- 'Referer: http://localhost:7456/'
- ]),
- 'content' => "\n\x03xy1\x12\x03123", // 与JS相同的二进制数据
- 'timeout' => 10,
- 'ignore_errors' => false
- ]
- ];
-
- $context = stream_context_create($options);
- try {
- $url = 'http://120.76.130.29:8080/ReqLogin';
- $context = stream_context_create($options);
- $response = file_get_contents($url, false, $context);
-
- if (strpos($response, '404 page not found') !== false) {
- throw new Exception("API端点不存在(404),请检查URL: " . $url);
- }
- $userResponse = new ResLogin();
- if ($response !== false) {
-
- $userResponse->mergeFromString($response);
- print_r($userResponse->getNikeName());
- 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();
- // 登录成功后才建立WebSocket连接
- $this->initWebSocketConnection();
-
- if ($userResponse->hasUserInfo()) {
- $this->enterHall($userResponse->getUserInfo()->getUserId());
- }
- } else {
- throw new Exception("请求失败: ".error_get_last()['message']);
- }
- } catch (Exception $e) {
- echo $e->getMessage()."\n";
- exit(1);
- }
- }
- private function initWebSocketConnection() {
- // 初始化WebSocket客户端
- $this->client = new BinaryWebSocketClient('https://leaf.ktle.top');
-
- // 注册消息处理器
- $this->client->on(MsgID::RESENTERHALL, function(ResEnterHall $response) {
- echo "成功进入大厅!\n";
- echo "大厅玩家数: ".$response->getSuccess()."\n";
- });
- // 开始监听消息
- $this->client->startListening();
- }
- private function enterHall($userId) {
- if (!$this->client) {
- throw new Exception("WebSocket连接未建立");
- }
- $enterHall = new EnterHall();
- $enterHall->setUserId($userId);
-
- $this->client->sendBinary(MsgID::ENTERHALL, $enterHall);
- echo "已发送进入大厅请求\n";
- }
- }
- // 启动客户端
- $gameClient = new GameClient();
- $gameClient->start();
|