This commit is contained in:
2026-04-04 10:34:16 +08:00
parent b77321ff4b
commit ee8ca1c4eb
+44 -33
View File
@@ -12,16 +12,28 @@ class Transport
/** /**
* @param int $fd * @param int $userId
* @param Struct $data * @param Struct $data
* @return void * @return void
*/ */
public function add(int $fd, Struct $data): void public function add(int $userId, Struct $data): void
{ {
if (isset($this->clients[$fd])) { if (isset($this->clients[$userId])) {
$this->clients[$fd]->ws->close(); $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 * @param mixed $data
* @return void * @return void
*/ */
public function send(int $fd, mixed $data): void public function sendFd(int $fd, mixed $data): void
{ {
if (isset($this->clients[$fd])) { $struct = $this->getClientId($fd);
$this->clients[$fd]->ws->push($data); $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 * @return void
*/ */
public function close(int $fd): void public function remove(int $userId): void
{ {
if (isset($this->clients[$fd])) { if ($this->has($userId)) {
$this->clients[$fd]->ws->close(); $this->clients[$userId]?->ws->close();
} }
} unset($this->clients[$userId]);
/**
* @param int $fd
* @return void
*/
public function remove(int $fd): void
{
if ($this->has($fd)) {
$this->clients[$fd]?->ws->close();
}
unset($this->clients[$fd]);
} }
@@ -73,22 +84,22 @@ class Transport
/** /**
* @param int $fd * @param int $userId
* @return Struct|null * @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 * @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 public function getLists(): array
{ {
$array = []; $array = [];
foreach ($this->clients as $fd => $client) { foreach ($this->clients as $userId => $client) {
$array[] = [ $array[] = [
'userId' => $fd, 'userId' => $userId,
'nickname' => $client->user->getNickname(), 'nickname' => $client->user->getNickname(),
]; ];
} }