From ca84671f33af98a0ed36513e3b729bca3b9bc329 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Thu, 22 Feb 2024 01:32:31 +0100 Subject: [PATCH] Implemented writeAll and closeAll for groups --- src/Group/ConnectionGroup.php | 16 ++++++++++++++-- src/Group/GroupManager.php | 6 ++++++ src/WebSocketConnection.php | 9 +++++++++ src/WebSocketInterface.php | 2 ++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Group/ConnectionGroup.php b/src/Group/ConnectionGroup.php index dbfb272..b4ccc10 100644 --- a/src/Group/ConnectionGroup.php +++ b/src/Group/ConnectionGroup.php @@ -61,8 +61,20 @@ class ConnectionGroup implements EventEmitterInterface, IteratorAggregate, Count return $this->name; } - public function write(string $payload) + public function writeAll(string $payload) { - + foreach ($this->connections as $connection) { + if ($connection->isWritable()) { + $connection->write($payload); + } + } } + + public function closeAll(string $reason, int $code=1001) + { + foreach ($this->connections as $connection) { + $connection->closeWithReason($reason, $code); + } + } + } \ No newline at end of file diff --git a/src/Group/GroupManager.php b/src/Group/GroupManager.php index fc60866..545065e 100644 --- a/src/Group/GroupManager.php +++ b/src/Group/GroupManager.php @@ -44,5 +44,11 @@ class GroupManager implements EventEmitterInterface return $group; } + public function closeAll(string $reason, int $code=1001) + { + foreach ($this->groups as $group) { + $group->closeAll($reason, $code); + } + } } \ No newline at end of file diff --git a/src/WebSocketConnection.php b/src/WebSocketConnection.php index bce482d..3fe23d1 100644 --- a/src/WebSocketConnection.php +++ b/src/WebSocketConnection.php @@ -252,8 +252,17 @@ class WebSocketConnection implements WebSocketInterface */ public function close() { + // TODO send close $this->outStream->close(); $this->inStream->close(); + // TODO emit close event + } + + public function closeWithReason(string $reason, int $code=1000) + { + // TODO send close + $payload = chr(($code >> 8) & 0xFF) . chr($code & 0xFF) . $reason; + $this->send(self::OP_CLOSE, $payload); } /** diff --git a/src/WebSocketInterface.php b/src/WebSocketInterface.php index 1ada6a9..6091596 100644 --- a/src/WebSocketInterface.php +++ b/src/WebSocketInterface.php @@ -23,4 +23,6 @@ interface WebSocketInterface extends ConnectionInterface public function getGroup(): ?ConnectionGroup; + public function closeWithReason(string $reason, int $code=1000); + } \ No newline at end of file