Added events to GroupManager
This commit is contained in:
parent
418ece5155
commit
a6f70dbb76
@ -15,4 +15,28 @@ foreach ($websocket->getGroup() as $other) {
|
|||||||
|
|
||||||
To remove a group from a connection, pass `null` to `WebSocketConnection::setGroup()`.
|
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.
|
||||||
|
@ -2,12 +2,19 @@
|
|||||||
|
|
||||||
namespace NoccyLabs\React\WebSocket\Group;
|
namespace NoccyLabs\React\WebSocket\Group;
|
||||||
|
|
||||||
|
use Evenement\EventEmitterInterface;
|
||||||
|
use Evenement\EventEmitterTrait;
|
||||||
use NoccyLabs\React\WebSocket\WebSocketInterface;
|
use NoccyLabs\React\WebSocket\WebSocketInterface;
|
||||||
use React\EventLoop\Loop;
|
use React\EventLoop\Loop;
|
||||||
use WeakReference;
|
use WeakReference;
|
||||||
|
|
||||||
class GroupManager
|
class GroupManager implements EventEmitterInterface
|
||||||
{
|
{
|
||||||
|
use EventEmitterTrait;
|
||||||
|
|
||||||
|
const EVENT_CREATED = 'created';
|
||||||
|
const EVENT_DESTROYED = 'destroyed';
|
||||||
|
|
||||||
/** @var array<string,ConnectionGroup> */
|
/** @var array<string,ConnectionGroup> */
|
||||||
private array $groups = [];
|
private array $groups = [];
|
||||||
|
|
||||||
@ -20,11 +27,13 @@ class GroupManager
|
|||||||
$group->on(ConnectionGroup::EVENT_LEAVE, function () use ($group) {
|
$group->on(ConnectionGroup::EVENT_LEAVE, function () use ($group) {
|
||||||
Loop::futureTick(function () use ($group) {
|
Loop::futureTick(function () use ($group) {
|
||||||
if (count($group) === 0) {
|
if (count($group) === 0) {
|
||||||
|
$this->emit(self::EVENT_DESTROYED, [ $group ]);
|
||||||
$group->removeAllListeners();
|
$group->removeAllListeners();
|
||||||
unset($this->groups[$group->getName()]);
|
unset($this->groups[$group->getName()]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
$this->emit(self::EVENT_CREATED, [ $group ]);
|
||||||
} else {
|
} else {
|
||||||
$group = $this->groups[$name];
|
$group = $this->groups[$name];
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user