Updated readme, example

This commit is contained in:
2024-02-24 14:18:26 +01:00
parent 899dd3b7e4
commit 7154b1baed
2 changed files with 8 additions and 7 deletions

View File

@ -19,16 +19,17 @@ $websockets->on('connection', function (WebSocketInterface $websocket) {
// This just echoes text received, unless the websocket is part of a group.
// In this case the message is sent to all websockets in the group.
$websocket->on('text', function ($text) use ($websocket) {
// Send a message with the group name starting with '#' to join a group.
if (str_starts_with($text, '#')) {
$websocket->setGroup(substr($text,1));
$websocket->write("joined group {$text}");
} else {
// Echo back if not in group, send to group otherwise
if (!$websocket->getGroup())
$websocket->write($text);
else
foreach ($websocket->getGroup() as $member) {
foreach ($websocket->getGroup() as $member)
$member->write($text);
}
}
});
});