|
@@ -18,7 +18,8 @@ class GameClient {
|
|
|
private ?WebSocketClient $client = null;
|
|
|
private ?string $userId = null;
|
|
|
private ?string $nikeName = null;
|
|
|
-
|
|
|
+ private ?int $listenerPid = null;
|
|
|
+ private bool $isRunning = false;
|
|
|
public function start() {
|
|
|
// $this->login();
|
|
|
}
|
|
@@ -127,8 +128,30 @@ class GameClient {
|
|
|
if (!$this->client) {
|
|
|
throw new Exception("WebSocket连接未建立");
|
|
|
}
|
|
|
+
|
|
|
+ if ($this->listenerPid !== null) {
|
|
|
+ return; // 已经启动监听
|
|
|
+ }
|
|
|
+
|
|
|
echo "开始监听WebSocket消息...\n";
|
|
|
- $this->client->startListening();
|
|
|
+
|
|
|
+ $pid = pcntl_fork();
|
|
|
+ if ($pid == -1) {
|
|
|
+ throw new Exception("无法创建子进程");
|
|
|
+ } elseif ($pid == 0) {
|
|
|
+ // 子进程:持续监听WebSocket
|
|
|
+ $this->isRunning = true;
|
|
|
+ $this->client->startListening();
|
|
|
+ exit(0);
|
|
|
+ } else {
|
|
|
+ // 父进程记录子进程ID
|
|
|
+ $this->listenerPid = $pid;
|
|
|
+ $this->isRunning = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function isRunning(): bool {
|
|
|
+ return $this->isRunning;
|
|
|
}
|
|
|
private function enterHall($userId) {
|
|
|
if (!$this->client) {
|