Initial commit
This commit is contained in:
35
src/Group/GroupManager.php
Normal file
35
src/Group/GroupManager.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\React\WebSocket\Group;
|
||||
|
||||
use NoccyLabs\React\WebSocket\WebSocketInterface;
|
||||
use React\EventLoop\Loop;
|
||||
use WeakReference;
|
||||
|
||||
class GroupManager
|
||||
{
|
||||
/** @var array<string,ConnectionGroup> */
|
||||
private array $groups = [];
|
||||
|
||||
public function getConnectionGroup(string $name): ConnectionGroup
|
||||
{
|
||||
if (!array_key_exists($name, $this->groups)) {
|
||||
$group = new ConnectionGroup($name);
|
||||
$this->groups[$name] = $group;
|
||||
// Clean up group when the last member leaves
|
||||
$group->on(ConnectionGroup::EVENT_LEAVE, function () use ($group) {
|
||||
Loop::futureTick(function () use ($group) {
|
||||
if (count($group) === 0) {
|
||||
$group->removeAllListeners();
|
||||
unset($this->groups[$group->getName()]);
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
$group = $this->groups[$name];
|
||||
}
|
||||
return $group;
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user