|
@@ -2,15 +2,20 @@
|
|
|
require __DIR__.'/../generated/vendor/autoload.php';
|
|
|
require_once __DIR__ . '/../vendor/autoload.php';
|
|
|
|
|
|
-use App\BinaryWebSocketClient;
|
|
|
+use App\WebSocketClient;
|
|
|
use Common_pack\EnterHall;
|
|
|
use Common_pack\ReqLogin;
|
|
|
use Common_pack\ResEnterHall;
|
|
|
use Common_pack\ResLogin;
|
|
|
use Google\Protobuf\Internal\Message;
|
|
|
|
|
|
+class MsgID {
|
|
|
+ const ENTERHALL = 1;
|
|
|
+ const RESENTERHALL = 2;
|
|
|
+}
|
|
|
+
|
|
|
class GameClient {
|
|
|
- private ?BinaryWebSocketClient $client = null;
|
|
|
+ private ?WebSocketClient $client = null;
|
|
|
private ?string $userId = null;
|
|
|
private ?string $nikeName = null;
|
|
|
|
|
@@ -29,7 +34,6 @@ class GameClient {
|
|
|
throw new Exception("序列化失败:返回空数据");
|
|
|
}
|
|
|
|
|
|
-
|
|
|
$options = [
|
|
|
'http' => [
|
|
|
'method' => 'POST',
|
|
@@ -40,66 +44,62 @@ class GameClient {
|
|
|
'Connection: keep-alive',
|
|
|
'Referer: http://localhost:7456/'
|
|
|
]),
|
|
|
- 'content' => "\n\x03xy1\x12\x03123", // 与JS相同的二进制数据
|
|
|
+ 'content' => $binaryData,
|
|
|
'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 ($response === false) {
|
|
|
+ throw new Exception("请求失败: " . error_get_last()['message']);
|
|
|
+ }
|
|
|
+
|
|
|
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());
|
|
|
- }
|
|
|
+ $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();
|
|
|
+ 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']);
|
|
|
+ $this->initWebSocketConnection();
|
|
|
+
|
|
|
+ if ($userResponse->hasUserInfo()) {
|
|
|
+ $this->enterHall($userResponse->getUserInfo()->getUserId());
|
|
|
}
|
|
|
+
|
|
|
} catch (Exception $e) {
|
|
|
- echo $e->getMessage()."\n";
|
|
|
+ echo $e->getMessage() . "\n";
|
|
|
exit(1);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private function initWebSocketConnection() {
|
|
|
- // 初始化WebSocket客户端
|
|
|
- $this->client = new BinaryWebSocketClient('https://leaf.ktle.top');
|
|
|
+ $this->client = new WebSocketClient('wss://leaf.ktle.top');
|
|
|
|
|
|
- // 注册消息处理器
|
|
|
- $this->client->on(MsgID::RESENTERHALL, function(ResEnterHall $response) {
|
|
|
+ $this->client->on(MsgID::RESENTERHALL, function($binaryData) {
|
|
|
+ $response = new ResEnterHall();
|
|
|
+ $response->mergeFromString($binaryData);
|
|
|
+
|
|
|
echo "成功进入大厅!\n";
|
|
|
- echo "大厅玩家数: ".$response->getSuccess()."\n";
|
|
|
+ echo "大厅玩家数: " . $response->getSuccess() . "\n";
|
|
|
});
|
|
|
|
|
|
- // 开始监听消息
|
|
|
$this->client->startListening();
|
|
|
}
|
|
|
|
|
@@ -111,7 +111,8 @@ class GameClient {
|
|
|
$enterHall = new EnterHall();
|
|
|
$enterHall->setUserId($userId);
|
|
|
|
|
|
- $this->client->sendBinary(MsgID::ENTERHALL, $enterHall);
|
|
|
+ $binaryData = $enterHall->serializeToString();
|
|
|
+ $this->client->sendBinary(MsgID::ENTERHALL, $binaryData);
|
|
|
echo "已发送进入大厅请求\n";
|
|
|
}
|
|
|
}
|