Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions src/WebSocket/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,4 @@ abstract public function setWorkerNumber(int $num): self;
* @return mixed
*/
abstract public function getNative(): mixed;

/**
* Returns all connections.
*
* @return array<mixed>
*/
abstract public function getConnections(): array;
}
13 changes: 0 additions & 13 deletions src/WebSocket/Adapter/Swoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class Swoole extends Adapter
protected string $host;

protected int $port;
/**
* @var array<int|string,bool|int|string>
*/
private static array $connections = [];

public function __construct(string $host = '0.0.0.0', int $port = 80)
{
Expand Down Expand Up @@ -103,8 +99,6 @@ public function onWorkerStop(callable $callback): Adapter
public function onOpen(callable $callback): self
{
$this->server->on('open', function (Server $server, Request $request) use ($callback) {
self::$connections[$request->fd] = true;

call_user_func($callback, $request->fd, $request);
});

Expand All @@ -123,8 +117,6 @@ public function onMessage(callable $callback): self
public function onClose(callable $callback): self
{
$this->server->on('close', function (Server $server, int $fd) use ($callback) {
unset(self::$connections[$fd]);

call_user_func($callback, $fd);
});

Expand Down Expand Up @@ -165,9 +157,4 @@ public function getNative(): Server
{
return $this->server;
}

public function getConnections(): array
{
return array_keys(self::$connections);
}
}
5 changes: 0 additions & 5 deletions src/WebSocket/Adapter/Workerman.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,4 @@ public function getNative(): Worker
{
return $this->server;
}

public function getConnections(): array
{
return array_keys(TcpConnection::$connections);
}
}
10 changes: 0 additions & 10 deletions src/WebSocket/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,6 @@ public function onRequest(callable $callback): self
return $this;
}

/**
* Returns all connections.
*
* @return array<mixed>
*/
public function getConnections(): array
{
return $this->adapter->getConnections();
}

/**
* Register callback. Will be executed when error occurs.
*
Expand Down
17 changes: 11 additions & 6 deletions tests/servers/Swoole/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,25 @@

$server = new WebSocket\Server($adapter);

/** @var array<int,bool> $connections */
$connections = [];

$server
->onWorkerStart(function (int $workerId) {
echo 'worker started ', $workerId, PHP_EOL;
})
->onWorkerStop(function (int $workerId) {
echo "worker stopped ", $workerId, PHP_EOL;
})
->onOpen(function (int $connection, Request $request) {
->onOpen(function (int $connection, Request $request) use (&$connections) {
$connections[$connection] = true;
echo 'connected ', $connection, PHP_EOL;
})
->onClose(function (int $connection) {
->onClose(function (int $connection) use (&$connections) {
unset($connections[$connection]);
echo 'disconnected ', $connection, PHP_EOL;
})
->onMessage(function (int $connection, string $message) use ($server) {
->onMessage(function (int $connection, string $message) use ($server, &$connections) {
echo $message, PHP_EOL;

switch ($message) {
Expand All @@ -35,15 +40,15 @@
$server->send([$connection], 'ping');
break;
case 'broadcast':
$server->send($server->getConnections(), 'broadcast');
$server->send(array_keys($connections), 'broadcast');
break;
case 'disconnect':
$server->send([$connection], 'disconnect');
$server->close($connection, 1000);
break;
}
})
->onRequest(function (Request $request, Response $response) use ($server) {
->onRequest(function (Request $request, Response $response) use (&$connections) {
echo 'HTTP request received: ', $request->server['request_uri'], PHP_EOL;

if ($request->server['request_uri'] === '/health') {
Expand All @@ -55,7 +60,7 @@
$response->status(200);
$response->end(json_encode([
'server' => 'Swoole WebSocket',
'connections' => count($server->getConnections()),
'connections' => count($connections),
'timestamp' => time()
]));
} else {
Expand Down
17 changes: 11 additions & 6 deletions tests/servers/Workerman/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,25 @@

$server = new WebSocket\Server($adapter);

/** @var array<int,bool> $connections */
$connections = [];

$server
->onWorkerStart(function (int $workerId) {
echo 'worker started ', $workerId, PHP_EOL;
})
->onWorkerStop(function (int $workerId) {
echo "worker stopped ", $workerId, PHP_EOL;
})
->onOpen(function (int $connection, array $request) {
->onOpen(function (int $connection, array $request) use (&$connections) {
$connections[$connection] = true;
echo 'connected ', $connection, PHP_EOL;
})
->onClose(function (int $connection) {
->onClose(function (int $connection) use (&$connections) {
unset($connections[$connection]);
echo 'disconnected ', $connection, PHP_EOL;
})
->onMessage(function (int $connection, string $message) use ($server) {
->onMessage(function (int $connection, string $message) use ($server, &$connections) {
echo $message, PHP_EOL;

switch ($message) {
Expand All @@ -35,15 +40,15 @@
$server->send([$connection], 'ping');
break;
case 'broadcast':
$server->send($server->getConnections(), 'broadcast');
$server->send(array_keys($connections), 'broadcast');
break;
case 'disconnect':
$server->send([$connection], 'disconnect');
$server->close($connection, 1000);
break;
}
})
->onRequest(function (TcpConnection $connection, Request $request) use ($server) {
->onRequest(function (TcpConnection $connection, Request $request) use (&$connections) {
$path = $request->path();
if (!is_string($path)) {
throw new \Exception('Invalid path ' . $path . ' for request: ' . json_encode($request, JSON_PRETTY_PRINT));
Expand All @@ -61,7 +66,7 @@
'Connection: close' . "\r\n\r\n" .
json_encode([
'server' => 'Workerman WebSocket',
'connections' => count($server->getConnections()),
'connections' => count($connections),
'timestamp' => time()
]));
} else {
Expand Down
Loading