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) ] diff --git a/src/LaravelEcho/WebSocket/EchoServer.php b/src/LaravelEcho/WebSocket/EchoServer.php index 45b706d..9f34499 100644 --- a/src/LaravelEcho/WebSocket/EchoServer.php +++ b/src/LaravelEcho/WebSocket/EchoServer.php @@ -46,9 +46,9 @@ class EchoServer extends WebSocketController ])); } - public function onMessage(ConnectionInterface $conn, MessageInterface $message) + public function onMessage(ConnectionInterface $connection, MessageInterface $message) { - $message = RespondableMessageFactory::createForMessage($message, $conn, $this->channelManager); + $message = RespondableMessageFactory::createForMessage($message, $connection, $this->channelManager); $message->respond(); }