Added events to GroupManager
This commit is contained in:
@ -15,4 +15,28 @@ foreach ($websocket->getGroup() as $other) {
|
||||
|
||||
To remove a group from a connection, pass `null` to `WebSocketConnection::setGroup()`.
|
||||
|
||||
The group will emit a `join` event (`WebSocketConnection::EVENT_JOIN`) when another member joins the group, and a `leave` event (`WebSocketConnection::EVENT_LEAVE`) when a member leaves. The events will be sent to the leaving member as well, so consider this in your logic.
|
||||
The group will emit a `join` event (`ConnectionGroup::EVENT_JOIN`) when another member joins the group, and a `leave` event (`ConnectionGroup::EVENT_LEAVE`) when a member leaves. The events will be sent to the leaving member as well, so consider this in your logic.
|
||||
|
||||
## Events
|
||||
|
||||
The GroupManager emits events when a group is `created` (`GroupManager::EVENT_CREATED`) or `destroyed` (`GroupManager::EVENT_DESTROYED`). You can use these events to hook the join and leave events.
|
||||
|
||||
```php
|
||||
// Create a GroupManager
|
||||
$groupManager = new GroupManager();
|
||||
$groupManager->on('created', function (ConnectionGroup $group) {
|
||||
// Listen for joins
|
||||
$group->on('join', function (WebSocketConnection $connection) use ($group) {
|
||||
// Someone joined the group!
|
||||
$group->write("Someone joined!");
|
||||
})
|
||||
});
|
||||
|
||||
// The GroupManager is injected into the WebSocketMiddleware
|
||||
$middleware = new WebSocketMiddleware($groupManager);
|
||||
```
|
||||
|
||||
## Future
|
||||
|
||||
* Add a GroupManagerImplementation so custom logic can be provided.
|
||||
* Make it possible to reject setting a group by GroupManager not returning a group.
|
||||
|
Reference in New Issue
Block a user