*/ private array $clients = []; /** * @param int $userId * @param Struct $data * @return void */ public function add(int $userId, Struct $data): void { 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); } } /** * @param int $fd * @param mixed $data * @return void */ public function sendFd(int $fd, mixed $data): void { $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 $userId * @return void */ public function remove(int $userId): void { if ($this->has($userId)) { $this->clients[$userId]?->ws->close(); } unset($this->clients[$userId]); } /** * @param int $fd * @return Struct|null */ public function getClientId(int $fd): ?Struct { return array_find($this->clients, fn($client) => $client->fd == $fd); } /** * @param int $userId * @return Struct|null */ public function getUserId(int $userId): ?Struct { return $this->clients[$userId] ?? null; } /** * @param int $userId * @return bool */ public function has(int $userId): bool { return isset($this->clients[$userId]); } /** * @return array */ public function getLists(): array { $array = []; foreach ($this->clients as $userId => $client) { $array[] = [ 'userId' => $userId, 'nickname' => $client->user->getNickname(), ]; } return $array; } /** * @return int */ public function size(): int { return count($this->clients); } }