From 54e228764657f8a276b577ff18ff1b6c067f8ca2 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Thu, 22 Nov 2018 22:02:36 +0100 Subject: [PATCH] wip --- .../Http/Controllers/EchoController.php | 6 +++++- .../Http/Controllers/FetchChannel.php | 14 ++++--------- src/LaravelEcho/Pusher/Channels/Channel.php | 20 ++++++++++++------ .../Pusher/Channels/PresenceChannel.php | 21 ++++++++++++------- 4 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/LaravelEcho/Http/Controllers/EchoController.php b/src/LaravelEcho/Http/Controllers/EchoController.php index 67bf27c..9dd6041 100644 --- a/src/LaravelEcho/Http/Controllers/EchoController.php +++ b/src/LaravelEcho/Http/Controllers/EchoController.php @@ -53,7 +53,11 @@ abstract class EchoController implements HttpServerInterface { if ($exception instanceof HttpException) { - $response = new Response($exception->getStatusCode(), $exception->getHeaders(), $exception->getMessage()); + $response = new Response($exception->getStatusCode(), [ + 'Content-Type' => 'application/json' + ], json_encode([ + 'error' => $exception->getMessage() + ])); $connection->send(gPsr\str($response)); $connection->close(); diff --git a/src/LaravelEcho/Http/Controllers/FetchChannel.php b/src/LaravelEcho/Http/Controllers/FetchChannel.php index 11bcdfd..54c56d7 100644 --- a/src/LaravelEcho/Http/Controllers/FetchChannel.php +++ b/src/LaravelEcho/Http/Controllers/FetchChannel.php @@ -19,18 +19,12 @@ class FetchChannel extends EchoController public function __invoke(Request $request) { - $this->verifySignature($request); + $channel = $this->channelManager->find($request->appId, $request->channelName); - foreach ($request->json()->get('channels', []) as $channelId) { - $channel = $this->channelManager->find($request->appId, $channelId); - - optional($channel)->broadcast([ - 'channel' => $channelId, - 'event' => $request->json()->get('name'), - 'data' => $request->json()->get('data'), - ]); + if (is_null($channel)) { + throw new HttpException(404, 'Unknown channel "'.$request->channelName.'"'); } - return $request->json()->all(); + return $channel->toArray(); } } \ No newline at end of file diff --git a/src/LaravelEcho/Pusher/Channels/Channel.php b/src/LaravelEcho/Pusher/Channels/Channel.php index deb8ca5..ff336ef 100644 --- a/src/LaravelEcho/Pusher/Channels/Channel.php +++ b/src/LaravelEcho/Pusher/Channels/Channel.php @@ -13,7 +13,7 @@ class Channel protected $channelId; /** @var \Ratchet\ConnectionInterface[] */ - protected $connections = []; + protected $subscriptions = []; public function __construct(string $channelId) { @@ -22,7 +22,7 @@ class Channel public function hasConnections(): bool { - return count($this->connections) > 0; + return count($this->subscriptions) > 0; } protected function verifySignature(ConnectionInterface $connection, stdClass $payload) @@ -56,25 +56,33 @@ class Channel public function unsubscribe(ConnectionInterface $connection) { - unset($this->connections[$connection->socketId]); + unset($this->subscriptions[$connection->socketId]); } protected function saveConnection(ConnectionInterface $connection) { - $this->connections[$connection->socketId] = $connection; + $this->subscriptions[$connection->socketId] = $connection; } public function broadcast($payload) { - foreach ($this->connections as $connection) { + foreach ($this->subscriptions as $connection) { $connection->send(json_encode($payload)); } } public function broadcastToOthers(ConnectionInterface $connection, $payload) { - Collection::make($this->connections)->reject(function ($existingConnection) use ($connection) { + Collection::make($this->subscriptions)->reject(function ($existingConnection) use ($connection) { return $existingConnection->socketId === $connection->socketId; })->each->send(json_encode($payload)); } + + public function toArray(): array + { + return [ + 'occupied' => count($this->subscriptions) > 0, + 'subscription_count' => count($this->subscriptions) + ]; + } } \ No newline at end of file diff --git a/src/LaravelEcho/Pusher/Channels/PresenceChannel.php b/src/LaravelEcho/Pusher/Channels/PresenceChannel.php index d7afb7a..160254e 100644 --- a/src/LaravelEcho/Pusher/Channels/PresenceChannel.php +++ b/src/LaravelEcho/Pusher/Channels/PresenceChannel.php @@ -6,7 +6,7 @@ use Ratchet\ConnectionInterface; class PresenceChannel extends Channel { - protected $subscriptions = []; + protected $users = []; /* * @link https://pusher.com/docs/pusher_protocol#presence-channel-events @@ -18,7 +18,7 @@ class PresenceChannel extends Channel $this->saveConnection($connection); $channelData = json_decode($payload->channel_data); - $this->subscriptions[$connection->socketId] = $channelData; + $this->users[$connection->socketId] = $channelData; // Send the success event $connection->send(json_encode([ @@ -42,21 +42,28 @@ class PresenceChannel extends Channel 'event' => 'pusher_internal:member_removed', 'channel' => $this->channelId, 'data' => json_encode([ - 'user_id' => $this->subscriptions[$connection->socketId]->user_id + 'user_id' => $this->users[$connection->socketId]->user_id ]) ]); - unset($this->subscriptions[$connection->socketId]); + unset($this->users[$connection->socketId]); } protected function getChannelData(): array { return [ 'presence' => [ - 'ids' => array_map(function($channelData) { return $channelData->user_id; }, $this->subscriptions), - 'hash' => array_map(function($channelData) { return $channelData->user_info; }, $this->subscriptions), - 'count' => count($this->subscriptions) + 'ids' => array_map(function($channelData) { return $channelData->user_id; }, $this->users), + 'hash' => array_map(function($channelData) { return $channelData->user_info; }, $this->users), + 'count' => count($this->users) ] ]; } + + public function toArray(): array + { + return array_merge(parent::toArray(),[ + 'user_count' => count($this->users), + ]); + } } \ No newline at end of file