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
20 changes: 20 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "CodeQL"

on: [pull_request]
jobs:
lint:
name: CodeQL
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 2

- run: git checkout HEAD^2

- name: Run CodeQL
run: |
docker run --rm -v $PWD:/app composer sh -c \
"composer install --profile --ignore-platform-reqs && composer check"
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"type": "library",
"keywords": ["php","framework", "upf", "utopia", "websocket"],
"license": "MIT",
"scripts": {
"check": "./vendor/bin/phpstan analyse --level max src tests"
},
"minimum-stability": "stable",
"autoload": {
"psr-4": {"Utopia\\WebSocket\\": "src/WebSocket"}
Expand All @@ -16,6 +19,7 @@
"textalk/websocket": "1.5.2",
"phpunit/phpunit": "^9.5.5",
"vimeo/psalm": "^4.8.1",
"workerman/workerman": "^4.0"
"workerman/workerman": "^4.0",
"phpstan/phpstan": "^1.8"
}
}
63 changes: 61 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/WebSocket/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ abstract class Adapter
{
protected string $host;
protected int $port;
/**
* @var array<int|string,bool|int|string>
*/

protected array $config = [];

function __construct(string $host = '0.0.0.0', int $port = 80) {
Expand All @@ -27,7 +31,7 @@ public abstract function shutdown(): void;

/**
* Sends a message to passed connections.
* @param array $connections Array of connection ID's.
* @param array<mixed,mixed> $connections Array of connection ID's.
* @param string $message Message.
* @return void
*/
Expand Down Expand Up @@ -105,7 +109,7 @@ public abstract function getNative(): mixed;

/**
* Returns all connections.
* @return array
* @return array<mixed>
*/
public abstract function getConnections(): array;
}
4 changes: 3 additions & 1 deletion src/WebSocket/Adapter/Swoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ 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
14 changes: 9 additions & 5 deletions src/WebSocket/Adapter/Workerman.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ public function __construct(string $host = '0.0.0.0', int $port = 80)

$this->server = new Worker("websocket://{$this->host}:{$this->port}");
}

public function start(): void
{
Worker::runAll();
call_user_func($this->callbackOnStart);
$callable = ($this->callbackOnStart);
if (!is_callable($callable)) {
throw new \Exception();
}
\call_user_func($callable);
}

public function shutdown(): void
{
Worker::stopAll();
}

public function send(array $connections, string $message): void
{
foreach ($connections as $connection) {
Expand Down Expand Up @@ -65,10 +69,10 @@ public function onWorkerStart(callable $callback): self

public function onOpen(callable $callback): self
{
$this->server->onConnect = function (mixed $connection) use ($callback): void {
$this->server->onConnect = function ($connection) use ($callback): void {
$connection->onWebSocketConnect = function(TcpConnection $connection) use ($callback): void
{
/** @var array $_SERVER */
/** @var array<string> $_SERVER */
call_user_func($callback, $connection->id, $_SERVER);
};
};
Expand Down
12 changes: 5 additions & 7 deletions src/WebSocket/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

class Server
{

/**
* Callbacks that will be executed when an error occurs
*
* @var array
*
* @var array<callable>
*/
protected $errorCallbacks = [];


protected Adapter $adapter;

/**
Expand Down Expand Up @@ -59,7 +59,7 @@ public function shutdown(): void

/**
* Sends a message to passed connections.
* @param array $connections Array of connection ID's.
* @param array<mixed,mixed> $connections Array of connection ID's.
* @param string $message Message.
* @return void
*/
Expand Down Expand Up @@ -182,8 +182,7 @@ public function onClose(callable $callback): self

/**
* Returns all connections.
* @param callable $callback
* @return array
* @return array<mixed>
*/
public function getConnections(): array
{
Expand All @@ -193,7 +192,6 @@ public function getConnections(): array
/**
* Register callback. Will be executed when error occurs.
* @param callable $callback
* @param Throwable $error
* @return self
*/
public function error(callable $callback): self
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testWorkerman(): void
$this->testServer(8002);
}

private function testServer(int $port)
private function testServer(int $port) :void
{
$client = $this->getWebsocket('localhost', $port);
$client->send('ping');
Expand Down