Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "Linter"

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

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

- run: git checkout HEAD^2

- name: Run Linter
run: |
docker run --rm -v $PWD:/app composer sh -c \
"composer install --profile --ignore-platform-reqs && composer lint"
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"autoload": {
"psr-4": {"Utopia\\WebSocket\\": "src/WebSocket"}
},
"scripts": {
"lint": "./vendor/bin/pint --test",
"format": "./vendor/bin/pint"
},
"require": {
"php": ">=8.0"
},
Expand All @@ -16,6 +20,7 @@
"textalk/websocket": "1.5.2",
"phpunit/phpunit": "^9.5.5",
"vimeo/psalm": "^4.8.1",
"workerman/workerman": "^4.0"
"workerman/workerman": "^4.0",
"laravel/pint": "1.2.*"
}
}
70 changes: 68 additions & 2 deletions composer.lock

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

103 changes: 60 additions & 43 deletions src/WebSocket/Adapter.php
Original file line number Diff line number Diff line change
@@ -1,111 +1,128 @@
<?php
namespace Utopia\WebSocket;

namespace Utopia\WebSocket;

abstract class Adapter
{
protected string $host;

protected int $port;

protected array $config = [];

function __construct(string $host = '0.0.0.0', int $port = 80) {
public function __construct(string $host = '0.0.0.0', int $port = 80)
{
$this->host = $host;
$this->port = $port;
}

/**
* Starts the Server.
* @return void
*
* @return void
*/
public abstract function start(): void;
abstract public function start(): void;

/**
* Shuts down the Server.
* @return void
*
* @return void
*/
public abstract function shutdown(): void;
abstract public function shutdown(): void;

/**
* Sends a message to passed connections.
* @param array $connections Array of connection ID's.
* @param string $message Message.
* @return void
*
* @param array $connections Array of connection ID's.
* @param string $message Message.
* @return void
*/
public abstract function send(array $connections, string $message): void;
abstract public function send(array $connections, string $message): void;

/**
* Closes a connection.
* @param int $connection Connection ID.
* @param int $code Close Code.
* @return void
*
* @param int $connection Connection ID.
* @param int $code Close Code.
* @return void
*/
public abstract function close(int $connection, int $code): void;
abstract public function close(int $connection, int $code): void;

/**
* Is called when the Server starts.
* @param callable $callback
* @return self
*
* @param callable $callback
* @return self
*/
public abstract function onStart(callable $callback): self;
abstract public function onStart(callable $callback): self;

/**
* Is called when a Worker starts.
* @param callable $callback
* @return self
*
* @param callable $callback
* @return self
*/
public abstract function onWorkerStart(callable $callback): self;
abstract public function onWorkerStart(callable $callback): self;

/**
* Is called when a connection is established.
* @param callable $callback
* @return self
*
* @param callable $callback
* @return self
*/
public abstract function onOpen(callable $callback): self;
abstract public function onOpen(callable $callback): self;

/**
* Is called when a message is received.
* @param callable $callback
* @return self
*
* @param callable $callback
* @return self
*/
public abstract function onMessage(callable $callback): self;
abstract public function onMessage(callable $callback): self;

/**
* Is called when a connection is closed.
* @param callable $callback
* @return self
*
* @param callable $callback
* @return self
*/
public abstract function onClose(callable $callback): self;
abstract public function onClose(callable $callback): self;

/**
* Sets maximum package length in bytes.
* @param int $bytes
* @return Adapter
*
* @param int $bytes
* @return Adapter
*/
public abstract function setPackageMaxLength(int $bytes): self;
abstract public function setPackageMaxLength(int $bytes): self;

/**
* Enables/Disables compression.
* @param bool $enabled
* @return Adapter
*
* @param bool $enabled
* @return Adapter
*/
public abstract function setCompressionEnabled(bool $enabled): self;
abstract public function setCompressionEnabled(bool $enabled): self;

/**
* Sets the number of workers.
* @param int $num
* @return Adapter
*
* @param int $num
* @return Adapter
*/
public abstract function setWorkerNumber(int $num): self;
abstract public function setWorkerNumber(int $num): self;

/**
* Returns the native server object from the Adapter.
* @return mixed
*
* @return mixed
*/
public abstract function getNative(): mixed;
abstract public function getNative(): mixed;

/**
* Returns all connections.
* @return array
*
* @return array
*/
public abstract function getConnections(): array;
}
abstract public function getConnections(): array;
}
17 changes: 11 additions & 6 deletions src/WebSocket/Adapter/Swoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
use Swoole\WebSocket\Server;
use Utopia\WebSocket\Adapter;

/**
*
* @package Utopia\WebSocket\Adapter
*/
class Swoole extends Adapter
{
protected Server $server;

protected string $host;

protected int $port;

private static array $connections = [];
Expand Down Expand Up @@ -74,14 +71,16 @@ public function onStart(callable $callback): self
$this->shutdown();
});
});

return $this;
}

public function onWorkerStart(callable $callback): self
{
$this->server->on('workerStart', function(Server $server, int $workerId) use ($callback) {
$this->server->on('workerStart', function (Server $server, int $workerId) use ($callback) {
call_user_func($callback, $workerId);
});

return $this;
}

Expand All @@ -92,6 +91,7 @@ public function onOpen(callable $callback): self

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

return $this;
}

Expand All @@ -100,6 +100,7 @@ public function onMessage(callable $callback): self
$this->server->on('message', function (Server $server, Frame $frame) use ($callback) {
call_user_func($callback, $frame->fd, $frame->data);
});

return $this;
}

Expand All @@ -110,28 +111,32 @@ public function onClose(callable $callback): self

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

return $this;
}

public function setPackageMaxLength(int $bytes): self
{
$this->config['package_max_length'] = $bytes;

return $this;
}

public function setCompressionEnabled(bool $enabled): self
{
$this->config['websocket_compression'] = $enabled;

return $this;
}

public function setWorkerNumber(int $num): self
{
$this->config['worker_num'] = $num;

return $this;
}

public function getNative(): \Swoole\WebSocket\Server
public function getNative(): Server
{
return $this->server;
}
Expand Down
Loading