From 5d8b49b60d2d100c30aeeb80728458561304fc36 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Thu, 22 Nov 2018 08:37:47 +0100 Subject: [PATCH] wip --- src/LaravelEcho/Pusher/Channels/Channel.php | 4 ++-- .../Pusher/Channels/PresenceChannel.php | 14 +++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/LaravelEcho/Pusher/Channels/Channel.php b/src/LaravelEcho/Pusher/Channels/Channel.php index 3ec6ef7..2e8d728 100644 --- a/src/LaravelEcho/Pusher/Channels/Channel.php +++ b/src/LaravelEcho/Pusher/Channels/Channel.php @@ -55,8 +55,8 @@ class Channel public function broadcastToOthers(ConnectionInterface $connection, $payload) { - Collection::make($this->connections)->reject(function ($connection) use ($connection) { - return $connection->socketId === $connection->socketId; + Collection::make($this->connections)->reject(function ($existingConnection) use ($connection) { + return $existingConnection->socketId === $connection->socketId; })->each->send(json_encode($payload)); } } \ No newline at end of file diff --git a/src/LaravelEcho/Pusher/Channels/PresenceChannel.php b/src/LaravelEcho/Pusher/Channels/PresenceChannel.php index 14233c0..b18cbb2 100644 --- a/src/LaravelEcho/Pusher/Channels/PresenceChannel.php +++ b/src/LaravelEcho/Pusher/Channels/PresenceChannel.php @@ -16,7 +16,7 @@ class PresenceChannel extends Channel $this->saveConnection($connection); $channelData = json_decode($payload->channel_data); - $this->subscriptions[$channelData->user_id] = $channelData; + $this->subscriptions[$connection->socketId] = $channelData; // Send the success event $connection->send(json_encode([ @@ -36,14 +36,22 @@ class PresenceChannel extends Channel { parent::unsubscribe($connection); - //TODO: send member_removed message back to client, and broadcast to everyone on channel + $this->broadcastToOthers($connection, [ + 'event' => 'pusher_internal:member_removed', + 'channel' => $this->channelId, + 'data' => json_encode([ + 'user_id' => $this->subscriptions[$connection->socketId]->user_id + ]) + ]); + + unset($this->subscriptions[$connection->socketId]); } protected function getChannelData(): array { return [ 'presence' => [ - 'ids' => array_keys($this->subscriptions), + '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) ]