Implemented writeAll and closeAll for groups

This commit is contained in:
Chris 2024-02-22 01:32:31 +01:00
parent b0aede55b9
commit ca84671f33
4 changed files with 31 additions and 2 deletions

View File

@ -61,8 +61,20 @@ class ConnectionGroup implements EventEmitterInterface, IteratorAggregate, Count
return $this->name; 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);
}
}
} }

View File

@ -44,5 +44,11 @@ class GroupManager implements EventEmitterInterface
return $group; return $group;
} }
public function closeAll(string $reason, int $code=1001)
{
foreach ($this->groups as $group) {
$group->closeAll($reason, $code);
}
}
} }

View File

@ -252,8 +252,17 @@ class WebSocketConnection implements WebSocketInterface
*/ */
public function close() public function close()
{ {
// TODO send close
$this->outStream->close(); $this->outStream->close();
$this->inStream->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);
} }
/** /**

View File

@ -23,4 +23,6 @@ interface WebSocketInterface extends ConnectionInterface
public function getGroup(): ?ConnectionGroup; public function getGroup(): ?ConnectionGroup;
public function closeWithReason(string $reason, int $code=1000);
} }