eee
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Coroutine\Server;
|
||||
|
||||
class Transport
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array<Struct>
|
||||
*/
|
||||
private array $clients = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @param Struct $data
|
||||
* @return void
|
||||
*/
|
||||
public function add(int $fd, Struct $data): void
|
||||
{
|
||||
if (isset($this->clients[$fd])) {
|
||||
$this->clients[$fd]->ws->close();
|
||||
}
|
||||
$this->clients[$fd] = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @param mixed $data
|
||||
* @return void
|
||||
*/
|
||||
public function send(int $fd, mixed $data): void
|
||||
{
|
||||
if (isset($this->clients[$fd])) {
|
||||
$this->clients[$fd]->ws->push($data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @return void
|
||||
*/
|
||||
public function close(int $fd): void
|
||||
{
|
||||
if (isset($this->clients[$fd])) {
|
||||
$this->clients[$fd]->ws->close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @return void
|
||||
*/
|
||||
public function remove(int $fd): void
|
||||
{
|
||||
if ($this->clients[$fd]) {
|
||||
$this->clients[$fd]->ws->close();
|
||||
}
|
||||
unset($this->clients[$fd]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getLists(): array
|
||||
{
|
||||
$array = [];
|
||||
foreach ($this->clients as $fd => $client) {
|
||||
$array[] = [
|
||||
'userId' => $fd,
|
||||
'nickname' => $client->user['nickname'],
|
||||
];
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function size(): int
|
||||
{
|
||||
return count($this->clients);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user