Renamed events in GroupManager

This commit is contained in:
Chris 2024-02-22 02:01:12 +01:00
parent 3abdced846
commit 054e052da7
3 changed files with 33 additions and 5 deletions

View File

@ -14,6 +14,14 @@ Ratchet is great! I've used Ratchet in the past, and it is a fantastic piece of
TL;DR - If you need to build an application with neatly wrapped classes without caring to much about the internals, go with Ratchet. If you want to work with websockets in the same way you work with sockets in ReactPHP, go with this library.
## Missing Features
The following features are missing, or work in progress:
* Idle timeout and ping timeout
* Protocol errors should close with error codes
* Exceptions
## Server
The WebSocket handler is built as a HttpServer middleware. This makes sense as WebSocket as a protocol is running over HTTP. Connections are set up by the middleware and exposed via the `connect` event.
@ -55,7 +63,7 @@ $http->listen($socket);
```
### Server Events
### WebSocketMiddleware Events
#### connection
@ -82,3 +90,15 @@ This event will be emitted when a binary data frame have been received and decod
#### close
#### error
### GroupManager events
### create
### destroy
### ConnectionGroup events
### join
### leave

View File

@ -36,6 +36,14 @@ $groupManager->on('created', function (ConnectionGroup $group) {
$middleware = new WebSocketMiddleware($groupManager);
```
## Sending messages
You can use the `ConnectionGoup::writeAll(string $payload)` method to send the payload to all members of the group.
## Disconnecting clients
You can disconnect clients cleanly on shutdown by using the `GroupManager::closeAll(string $reason, int $code)` method. You can also call on `ConnectionGrroup::closeAll` manually do disconnect a whole group.
## Future
* Add a GroupManagerImplementation so custom logic can be provided.

View File

@ -13,11 +13,11 @@ class GroupManager implements EventEmitterInterface
/**
* @var string emitted when a new group is created
*/
const EVENT_CREATED = 'created';
const EVENT_CREATE = 'create';
/**
* @var string emitted after the last member leaves, when the group is destroyed
*/
const EVENT_DESTROYED = 'destroyed';
const EVENT_DESTROY = 'destroy';
/** @var array<string,ConnectionGroup> */
private array $groups = [];
@ -31,13 +31,13 @@ class GroupManager implements EventEmitterInterface
$group->on(ConnectionGroup::EVENT_LEAVE, function () use ($group) {
Loop::futureTick(function () use ($group) {
if (count($group) === 0) {
$this->emit(self::EVENT_DESTROYED, [ $group ]);
$this->emit(self::EVENT_DESTROY, [ $group ]);
$group->removeAllListeners();
unset($this->groups[$group->getName()]);
}
});
});
$this->emit(self::EVENT_CREATED, [ $group ]);
$this->emit(self::EVENT_CREATE, [ $group ]);
} else {
$group = $this->groups[$name];
}