From ee8ca1c4eb14855e0485bb3543efadc41a6b3791 Mon Sep 17 00:00:00 2001 From: whwyy Date: Sat, 4 Apr 2026 10:34:16 +0800 Subject: [PATCH] eee --- core/Transport.php | 77 ++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/core/Transport.php b/core/Transport.php index 595fd7d..904d136 100644 --- a/core/Transport.php +++ b/core/Transport.php @@ -12,16 +12,28 @@ class Transport /** - * @param int $fd + * @param int $userId * @param Struct $data * @return void */ - public function add(int $fd, Struct $data): void + public function add(int $userId, Struct $data): void { - if (isset($this->clients[$fd])) { - $this->clients[$fd]->ws->close(); + if (isset($this->clients[$userId])) { + $this->clients[$userId]->ws->close(); + } + $this->clients[$userId] = $data; + } + + /** + * @param int $userId + * @param mixed $data + * @return void + */ + public function sendUserId(int $userId, mixed $data): void + { + if (isset($this->clients[$userId])) { + $this->clients[$userId]->ws->push($data); } - $this->clients[$fd] = $data; } /** @@ -29,36 +41,35 @@ class Transport * @param mixed $data * @return void */ - public function send(int $fd, mixed $data): void + public function sendFd(int $fd, mixed $data): void { - if (isset($this->clients[$fd])) { - $this->clients[$fd]->ws->push($data); + $struct = $this->getClientId($fd); + $struct?->ws->push($data); + } + + + /** + * @param int $userId + * @return void + */ + public function close(int $userId): void + { + if (isset($this->clients[$userId])) { + $this->clients[$userId]->ws->close(); } } /** - * @param int $fd + * @param int $userId * @return void */ - public function close(int $fd): void + public function remove(int $userId): void { - if (isset($this->clients[$fd])) { - $this->clients[$fd]->ws->close(); + if ($this->has($userId)) { + $this->clients[$userId]?->ws->close(); } - } - - - /** - * @param int $fd - * @return void - */ - public function remove(int $fd): void - { - if ($this->has($fd)) { - $this->clients[$fd]?->ws->close(); - } - unset($this->clients[$fd]); + unset($this->clients[$userId]); } @@ -73,22 +84,22 @@ class Transport /** - * @param int $fd + * @param int $userId * @return Struct|null */ - public function getUserId(int $fd): ?Struct + public function getUserId(int $userId): ?Struct { - return $this->clients[$fd] ?? null; + return $this->clients[$userId] ?? null; } /** - * @param int $fd + * @param int $userId * @return bool */ - public function has(int $fd): bool + public function has(int $userId): bool { - return isset($this->clients[$fd]); + return isset($this->clients[$userId]); } @@ -98,9 +109,9 @@ class Transport public function getLists(): array { $array = []; - foreach ($this->clients as $fd => $client) { + foreach ($this->clients as $userId => $client) { $array[] = [ - 'userId' => $fd, + 'userId' => $userId, 'nickname' => $client->user->getNickname(), ]; }