diff --git a/composer.json b/composer.json index f9c84b5..24dd1e4 100644 --- a/composer.json +++ b/composer.json @@ -3,6 +3,7 @@ "description": "Native ReactPHP WebSocket implementation", "type": "library", "license": "GPL-3.0-or-later", + "keywords": [ "reactphp", "websockets" ], "autoload": { "psr-4": { "NoccyLabs\\React\\WebSocket\\": "src/" @@ -18,6 +19,7 @@ "react/http": "^1.9.0" }, "require-dev": { - "phpunit/phpunit": "^11.0" + "phpunit/phpunit": "^11.0", + "phpstan/phpstan": "^1.10" } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..e68bade --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,12 @@ +parameters: + level: 5 + + excludePaths: + - doc + - vendor + - tests + + # Paths to include in the analysis + paths: + - src + diff --git a/src/Group/ConnectionGroup.php b/src/Group/ConnectionGroup.php index 0176957..dbfb272 100644 --- a/src/Group/ConnectionGroup.php +++ b/src/Group/ConnectionGroup.php @@ -60,4 +60,9 @@ class ConnectionGroup implements EventEmitterInterface, IteratorAggregate, Count { return $this->name; } + + public function write(string $payload) + { + + } } \ No newline at end of file diff --git a/src/Group/GroupManager.php b/src/Group/GroupManager.php index ab9870d..fc60866 100644 --- a/src/Group/GroupManager.php +++ b/src/Group/GroupManager.php @@ -4,15 +4,19 @@ namespace NoccyLabs\React\WebSocket\Group; use Evenement\EventEmitterInterface; use Evenement\EventEmitterTrait; -use NoccyLabs\React\WebSocket\WebSocketInterface; use React\EventLoop\Loop; -use WeakReference; class GroupManager implements EventEmitterInterface { use EventEmitterTrait; + /** + * @var string emitted when a new group is created + */ const EVENT_CREATED = 'created'; + /** + * @var string emitted after the last member leaves, when the group is destroyed + */ const EVENT_DESTROYED = 'destroyed'; /** @var array */ diff --git a/src/WebSocketConnection.php b/src/WebSocketConnection.php index ad0bf32..a9be681 100644 --- a/src/WebSocketConnection.php +++ b/src/WebSocketConnection.php @@ -54,6 +54,10 @@ class WebSocketConnection implements WebSocketInterface $this->groupManager = $groupManager; $this->inStream->on('data', $this->onWebSocketData(...)); + $this->inStream->on('close', function () { + $this->close(); + $this->emit('close', []); + }); } private function onWebSocketData($data) @@ -87,6 +91,7 @@ class WebSocketConnection implements WebSocketInterface $this->sendPong($payload); return; case self::OP_PONG: + $this->checkPong($payload); return; case self::OP_CLOSE: // TODO implement @@ -103,11 +108,27 @@ class WebSocketConnection implements WebSocketInterface } } + /** + * Sends a ping, and closes the connection on timeout. + * + */ + public function ping(): void + { + // TODO save the state somehow + $payload = "ping"; + $this->send(self::OP_PING, $payload, true); + } + private function sendPong(string $data): void { $this->send(self::OP_PONG, $data, true); } + private function checkPong(string $data): void + { + // TODO reset the state and any ping timers + } + public function setGroup(?string $name): void { if ($this->group) { @@ -215,24 +236,4 @@ class WebSocketConnection implements WebSocketInterface } - // private function hexdump($data): void - // { - // printf("%4d .\n", strlen($data)); - // $rows = str_split($data, 16); - // $offs = 0; - // foreach ($rows as $row) { - // $h = []; $a = []; - // for ($n = 0; $n < 16; $n++) { - // if ($n < strlen($row)) { - // $h[] = sprintf("%02x%s", ord($row[$n]), ($n==7)?" ":" "); - // $a[] = sprintf("%s%s", (ctype_print($row[$n])?$row[$n]:"."), ($n==7)?" ":""); - // } else { - // $h[] = (($n==7)?" ":" "); - // $a[] = (($n==7)?" ":" "); - // } - // } - // printf("%04x | %s | %s\n", 16 * $offs++, join("", $h), join("", $a)); - // } - // } - } \ No newline at end of file