eee
This commit is contained in:
@@ -14,6 +14,12 @@ class Transport
|
||||
*/
|
||||
private array $userIdByFd = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
* @param Struct $data
|
||||
* @return Struct|null
|
||||
*/
|
||||
public function add(int $userId, Struct $data): ?Struct
|
||||
{
|
||||
$previous = $this->clients[$userId] ?? null;
|
||||
@@ -31,6 +37,12 @@ class Transport
|
||||
return $previous;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
* @param mixed $data
|
||||
* @return void
|
||||
*/
|
||||
public function sendUserId(int $userId, mixed $data): void
|
||||
{
|
||||
$struct = $this->clients[$userId] ?? null;
|
||||
@@ -42,6 +54,12 @@ class Transport
|
||||
@$struct->ws->push($this->normalizePayload($data));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @param mixed $data
|
||||
* @return void
|
||||
*/
|
||||
public function sendFd(int $fd, mixed $data): void
|
||||
{
|
||||
$struct = $this->getClientId($fd);
|
||||
@@ -53,6 +71,11 @@ class Transport
|
||||
@$struct->ws->push($this->normalizePayload($data));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
* @return void
|
||||
*/
|
||||
public function close(int $userId): void
|
||||
{
|
||||
$struct = $this->clients[$userId] ?? null;
|
||||
@@ -63,6 +86,12 @@ class Transport
|
||||
@$struct->ws->close();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
* @param int|null $fd
|
||||
* @return void
|
||||
*/
|
||||
public function remove(int $userId, ?int $fd = null): void
|
||||
{
|
||||
$struct = $this->clients[$userId] ?? null;
|
||||
@@ -80,6 +109,11 @@ class Transport
|
||||
@$struct->ws->close();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @return Struct|null
|
||||
*/
|
||||
public function getClientId(int $fd): ?Struct
|
||||
{
|
||||
$userId = $this->userIdByFd[$fd] ?? null;
|
||||
@@ -90,16 +124,31 @@ class Transport
|
||||
return $this->clients[$userId] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
* @param int|null $fd
|
||||
* @return void
|
||||
*/
|
||||
public function touch(int $userId, ?int $fd = null): void
|
||||
{
|
||||
$struct = $this->clients[$userId] ?? null;
|
||||
@@ -114,6 +163,10 @@ class Transport
|
||||
$struct->touch();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getLists(): array
|
||||
{
|
||||
$array = [];
|
||||
@@ -130,11 +183,20 @@ class Transport
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function size(): int
|
||||
{
|
||||
return count($this->clients);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
*/
|
||||
private function normalizePayload(mixed $data): string
|
||||
{
|
||||
if (is_string($data)) {
|
||||
|
||||
Reference in New Issue
Block a user