From 0b8e09e39ebecb64631e00e884c3b82043ea0757 Mon Sep 17 00:00:00 2001 From: freek Date: Wed, 21 Nov 2018 22:11:44 +0100 Subject: [PATCH] commit --- .../Http/Controllers/EventController.php | 16 +++++++++--- src/LaravelEcho/Pusher/Channels/Channel.php | 3 ++- .../Pusher/Channels/ChannelManager.php | 25 +++++++++++++------ src/LaravelEcho/WebSocket/EchoServer.php | 15 +++++++++-- 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/LaravelEcho/Http/Controllers/EventController.php b/src/LaravelEcho/Http/Controllers/EventController.php index 1df4fc1..ff98864 100644 --- a/src/LaravelEcho/Http/Controllers/EventController.php +++ b/src/LaravelEcho/Http/Controllers/EventController.php @@ -7,9 +7,8 @@ use Illuminate\Http\Request; class EventController extends EchoController { - /** @var ChannelManager */ - private $channelManager; + protected $channelManager; public function __construct(ChannelManager $channelManager) { @@ -18,8 +17,19 @@ class EventController extends EchoController public function __invoke(Request $request) { + //TODO: verify the incoming request + /* + * array:6 [ + "appId" => "test" + "auth_key" => "" + "auth_signature" => "51e7ab9c1411aacf9a4c28001ffc3e7f5fe71db130ce08ac071ab49d737bcf52" + "auth_timestamp" => "1542833998" + "auth_version" => "1.0" + "body_md5" => "816e28da10f4aedf0821865eddf55e7f" +] + */ foreach ($request->json()->get('channels', []) as $channelId) { - $channel = $this->channelManager->find($channelId); + $channel = $this->channelManager->find($request->appId, $channelId); $channel->broadcast([ 'channel' => $channelId, diff --git a/src/LaravelEcho/Pusher/Channels/Channel.php b/src/LaravelEcho/Pusher/Channels/Channel.php index 98a1fb7..58f70d2 100644 --- a/src/LaravelEcho/Pusher/Channels/Channel.php +++ b/src/LaravelEcho/Pusher/Channels/Channel.php @@ -7,12 +7,13 @@ use Ratchet\ConnectionInterface; class Channel { + /** @var string */ protected $channelId; /** @var ConnectionInterface[] */ protected $connections = []; - public function __construct($channelId) + public function __construct(string $channelId) { $this->channelId = $channelId; } diff --git a/src/LaravelEcho/Pusher/Channels/ChannelManager.php b/src/LaravelEcho/Pusher/Channels/ChannelManager.php index 16fa54f..1ccd3f6 100644 --- a/src/LaravelEcho/Pusher/Channels/ChannelManager.php +++ b/src/LaravelEcho/Pusher/Channels/ChannelManager.php @@ -5,28 +5,39 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels; class ChannelManager { + /** @var array */ protected $channels = []; - public function findOrCreate(string $channelId): Channel + /** @var string */ + protected $appId; + + public function findOrCreate(string $appId, string $channelId): Channel { - if (! isset($this->channels[$channelId])) { + + if (! isset($this->channels[$appId][$channelId])) { + + /**TODO: make this variable to go away */ $channelClass = $this->detectChannelClass($channelId); - $this->channels[$channelId] = new $channelClass($channelId); + + array_set($this->channels, "{$appId}.{$channelId}", new $channelClass($channelId)); + } - return $this->channels[$channelId]; + return $this->channels[$appId][$channelId]; } - public function find(string $channelId) + public function find(string $appId, string $channelId) { - return $this->channels[$channelId] ?? null; + return $this->channels[$appId][$channelId] ?? null; } protected function detectChannelClass($channelId) : string { if (starts_with($channelId, 'private-')) { return PrivateChannel::class; - } elseif(starts_with($channelId, 'presence-')) { + } + + if(starts_with($channelId, 'presence-')) { return PresenceChannel::class; } return Channel::class; diff --git a/src/LaravelEcho/WebSocket/EchoServer.php b/src/LaravelEcho/WebSocket/EchoServer.php index 4b0e216..4a73cb9 100644 --- a/src/LaravelEcho/WebSocket/EchoServer.php +++ b/src/LaravelEcho/WebSocket/EchoServer.php @@ -24,6 +24,7 @@ class EchoServer extends WebSocketController */ function onOpen(ConnectionInterface $conn) { + dump("Client connected"); /** * There are a couple things we need to do here: @@ -35,6 +36,14 @@ class EchoServer extends WebSocketController // Store the socketId along with the connection so we can retrieve it. $conn->socketId = $socketId; + /** @var \GuzzleHttp\Psr7\Request $request */ + $request = $conn->httpRequest; + + $queryParameters = []; + parse_str($request->getUri()->getQuery(), $queryParameters); + + $conn->appId = $queryParameters['appId']; + $conn->send($this->buildPayload('pusher:connection_established', [ 'socket_id' => $socketId, 'activity_timeout' => 60, @@ -58,7 +67,8 @@ class EchoServer extends WebSocketController } } else { // Try to find a channel and broadcast the message to the clients. - $channel = $this->channelManager->find($payload->channel); + $channel = $this->channelManager->find($conn->appId, $payload->channel); + if ($channel) { $channel->broadcast($payload); } @@ -82,7 +92,8 @@ class EchoServer extends WebSocketController */ protected function pusherSubscribe(ConnectionInterface $conn, $payload) { - $channel = $this->channelManager->findOrCreate($payload->channel); + $channel = $this->channelManager->findOrCreate($conn->appId, $payload->channel); + $channel->subscribe($conn, $payload); }