1
0
Fork 0
mirror of synced 2024-07-03 05:31:38 +12:00

add remove subscriptions to class

This commit is contained in:
Torsten Dittmann 2021-02-26 18:26:22 +01:00
parent ffb275d46d
commit 8ef300c020
5 changed files with 75 additions and 31 deletions

View file

@ -294,32 +294,7 @@ $server->on('message', function (Server $server, Frame $frame) {
}); });
$server->on('close', function (Server $server, int $fd) use (&$connections, &$subscriptions) { $server->on('close', function (Server $server, int $fd) use (&$connections, &$subscriptions) {
/** Realtime::removeSubscription($fd, $subscriptions, $connections);
* TODO: Move into Realtime Class for tests
*/
$projectId = $connections[$fd]['projectId'] ?? '';
$roles = $connections[$fd]['roles'] ?? [];
foreach ($roles as $key => $role) {
foreach ($subscriptions[$projectId][$role] as $channel => $list) {
unset($subscriptions[$projectId][$role][$channel][$fd]); // Remove connection
if (empty($subscriptions[$projectId][$role][$channel])) {
unset($subscriptions[$projectId][$role][$channel]); // Remove channel when no connections
}
}
if (empty($subscriptions[$projectId][$role])) {
unset($subscriptions[$projectId][$role]); // Remove role when no channels
}
}
if (empty($subscriptions[$projectId])) { // Remove project when no roles
unset($subscriptions[$projectId]);
}
unset($connections[$fd]);
Console::info('Connection close: ' . $fd); Console::info('Connection close: ' . $fd);
}); });

View file

@ -110,7 +110,7 @@ class Realtime
* @param array $roles * @param array $roles
* @param array $channels * @param array $channels
*/ */
static function addSubscription($projectId, $connection, &$subscriptions, &$connections, &$roles, &$channels) static function addSubscription($projectId, $connection, $roles, &$subscriptions, &$connections, &$channels)
{ {
/** /**
* Build Subscriptions Tree * Build Subscriptions Tree
@ -145,4 +145,37 @@ class Realtime
'roles' => $roles, 'roles' => $roles,
]; ];
} }
/**
* Remove Subscription.
*
* @param mixed $connection
* @param array $subscriptions
* @param array $connections
*/
static function removeSubscription($connection, &$subscriptions, &$connections)
{
$projectId = $connections[$connection]['projectId'] ?? '';
$roles = $connections[$connection]['roles'] ?? [];
foreach ($roles as $key => $role) {
foreach ($subscriptions[$projectId][$role] as $channel => $list) {
unset($subscriptions[$projectId][$role][$channel][$connection]); // Remove connection
if (empty($subscriptions[$projectId][$role][$channel])) {
unset($subscriptions[$projectId][$role][$channel]); // Remove channel when no connections
}
}
if (empty($subscriptions[$projectId][$role])) {
unset($subscriptions[$projectId][$role]); // Remove role when no channels
}
}
if (empty($subscriptions[$projectId])) { // Remove project when no roles
unset($subscriptions[$projectId]);
}
unset($connections[$connection]);
}
} }

View file

@ -63,9 +63,9 @@ class RealtimeChannelsTest extends TestCase
Realtime::addSubscription( Realtime::addSubscription(
'1', '1',
$this->connectionsCount, $this->connectionsCount,
Realtime::getRoles(),
$this->subscriptions, $this->subscriptions,
$this->connections, $this->connections,
Realtime::getRoles(),
Realtime::parseChannels([0 => $channel]) Realtime::parseChannels([0 => $channel])
); );
@ -85,9 +85,9 @@ class RealtimeChannelsTest extends TestCase
Realtime::addSubscription( Realtime::addSubscription(
'1', '1',
$this->connectionsCount, $this->connectionsCount,
Realtime::getRoles(),
$this->subscriptions, $this->subscriptions,
$this->connections, $this->connections,
Realtime::getRoles(),
Realtime::parseChannels([0 => $channel]) Realtime::parseChannels([0 => $channel])
); );
@ -127,6 +127,20 @@ class RealtimeChannelsTest extends TestCase
* - Guests * - Guests
*/ */
$this->assertCount($this->connectionsTotal, $this->connections); $this->assertCount($this->connectionsTotal, $this->connections);
Realtime::removeSubscription(-1, $this->subscriptions, $this->connections);
$this->assertCount($this->connectionsTotal, $this->connections);
$this->assertCount(($this->connectionsAuthenticated + (3 * $this->connectionsPerChannel) + 3), $this->subscriptions['1']);
for ($i=0; $i < $this->connectionsCount; $i++) {
Realtime::removeSubscription($i, $this->subscriptions, $this->connections);
$this->assertCount(($this->connectionsCount - $i - 1), $this->connections);
}
$this->assertEmpty($this->connections);
$this->assertEmpty($this->subscriptions);
} }
/** /**

View file

@ -46,7 +46,7 @@ class RealtimeGuestTest extends TestCase
$this->assertArrayNotHasKey('account', $channels); $this->assertArrayNotHasKey('account', $channels);
$this->assertArrayNotHasKey('account.456', $channels); $this->assertArrayNotHasKey('account.456', $channels);
Realtime::addSubscription('1', 1, $this->subscriptions, $this->connections, $roles, $channels); Realtime::addSubscription('1', 1, $roles, $this->subscriptions, $this->connections, $channels);
$event = [ $event = [
@ -201,5 +201,16 @@ class RealtimeGuestTest extends TestCase
); );
$this->assertEmpty($receivers); $this->assertEmpty($receivers);
Realtime::removeSubscription(2, $this->subscriptions, $this->connections);
$this->assertCount(1, $this->connections);
$this->assertCount(2, $this->subscriptions['1']);
Realtime::removeSubscription(1, $this->subscriptions, $this->connections);
$this->assertEmpty($this->connections);
$this->assertEmpty($this->subscriptions);
} }
} }

View file

@ -70,7 +70,7 @@ class RealtimeTest extends TestCase
$this->assertArrayNotHasKey('account', $channels); $this->assertArrayNotHasKey('account', $channels);
$this->assertArrayNotHasKey('account.456', $channels); $this->assertArrayNotHasKey('account.456', $channels);
Realtime::addSubscription('1', 1, $this->subscriptions, $this->connections, $roles, $channels); Realtime::addSubscription('1', 1, $roles, $this->subscriptions, $this->connections, $channels);
$event = [ $event = [
'project' => '1', 'project' => '1',
@ -219,5 +219,16 @@ class RealtimeTest extends TestCase
); );
$this->assertEmpty($receivers); $this->assertEmpty($receivers);
Realtime::removeSubscription(2, $this->subscriptions, $this->connections);
$this->assertCount(1, $this->connections);
$this->assertCount(8, $this->subscriptions['1']);
Realtime::removeSubscription(1, $this->subscriptions, $this->connections);
$this->assertEmpty($this->connections);
$this->assertEmpty($this->subscriptions);
} }
} }