This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 639
Update ChannelManager #41
Merged
mpociot
merged 5 commits into
beyondcode:master
from
snellingio:allow-swapping-of-channel-manager
Dec 10, 2018
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a5fd89b
Update Channel Manager
snellingio 2358dd1
Add a fallback if the config variable isn’t set
snellingio c088752
Fix StyleCI
snellingio 22c6c8d
Fix StyleCI line break
snellingio 66e02f7
Add description for the ChannelManager option
snellingio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/WebSockets/Channels/ChannelManagers/ArrayChannelManager.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers; | ||
|
||
use Ratchet\ConnectionInterface; | ||
use BeyondCode\LaravelWebSockets\WebSockets\Channels\Channel; | ||
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; | ||
use BeyondCode\LaravelWebSockets\WebSockets\Channels\PrivateChannel; | ||
use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel; | ||
|
||
class ArrayChannelManager implements ChannelManager | ||
{ | ||
/** @var string */ | ||
protected $appId; | ||
|
||
/** @var array */ | ||
protected $channels = []; | ||
|
||
public function findOrCreate(string $appId, string $channelName): Channel | ||
{ | ||
if (! isset($this->channels[$appId][$channelName])) { | ||
$channelClass = $this->determineChannelClass($channelName); | ||
|
||
$this->channels[$appId][$channelName] = new $channelClass($channelName); | ||
} | ||
|
||
return $this->channels[$appId][$channelName]; | ||
} | ||
|
||
public function find(string $appId, string $channelName): ?Channel | ||
{ | ||
return $this->channels[$appId][$channelName] ?? null; | ||
} | ||
|
||
protected function determineChannelClass(string $channelName): string | ||
{ | ||
if (starts_with($channelName, 'private-')) { | ||
return PrivateChannel::class; | ||
} | ||
|
||
if (starts_with($channelName, 'presence-')) { | ||
return PresenceChannel::class; | ||
} | ||
|
||
return Channel::class; | ||
} | ||
|
||
public function getChannels(string $appId): array | ||
{ | ||
return $this->channels[$appId] ?? []; | ||
} | ||
|
||
public function getConnectionCount(string $appId): int | ||
{ | ||
return collect($this->getChannels($appId)) | ||
->sum(function ($channel) { | ||
return count($channel->getSubscribedConnections()); | ||
}); | ||
} | ||
|
||
public function removeFromAllChannels(ConnectionInterface $connection) | ||
{ | ||
if (! isset($connection->app)) { | ||
return; | ||
} | ||
|
||
/* | ||
* Remove the connection from all channels. | ||
*/ | ||
collect(array_get($this->channels, $connection->app->id, []))->each->unsubscribe($connection); | ||
|
||
/* | ||
* Unset all channels that have no connections so we don't leak memory. | ||
*/ | ||
collect(array_get($this->channels, $connection->app->id, [])) | ||
->reject->hasConnections() | ||
->each(function (Channel $channel, string $channelName) use ($connection) { | ||
unset($this->channels[$connection->app->id][$channelName]); | ||
}); | ||
|
||
if (count(array_get($this->channels, $connection->app->id, [])) === 0) { | ||
unset($this->channels[$connection->app->id]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add some more info here on what this does and in which cases you would change this option
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.