From 32c628a9361a626e8d473e104d987c10bf31a5cb Mon Sep 17 00:00:00 2001 From: a6a2f5842 Date: Thu, 18 Sep 2025 15:23:14 +0200 Subject: [PATCH] I OpenPresenceChannel --- src/Channels/OpenPresenceChannel.php | 72 +++++++++++++--------------- 1 file changed, 32 insertions(+), 40 deletions(-) diff --git a/src/Channels/OpenPresenceChannel.php b/src/Channels/OpenPresenceChannel.php index 0f79df9..315faee 100644 --- a/src/Channels/OpenPresenceChannel.php +++ b/src/Channels/OpenPresenceChannel.php @@ -40,26 +40,7 @@ class OpenPresenceChannel extends Channel ], ])); - $memberAddedPayload = [ - 'event' => 'presence.member_added', - 'channel' => $this->getName(), - 'data' => [ - 'socket' => $connection->socketId, // added socket - 'total_count' => collect($connections) - ->filter(fn($conn) => ($conn->remoteAddress && $conn->remoteAddress != '127.0.0.1')) - ->count(), - ], - ]; - - - if ($connection->remoteAddress && $connection->remoteAddress != '127.0.0.1') { - $this->broadcastToEveryoneExcept( - (object) $memberAddedPayload, - $connection->socketId, - $connection->app->id, - false - ); - } + $this->informPresence($connection, $connections); }); return true; @@ -75,32 +56,43 @@ class OpenPresenceChannel extends Channel { $truth = parent::unsubscribe($connection); - return $this->channelManager ->getLocalConnections() ->then(function ($connections) use ($connection) { - $memberRemovedPayload = [ - 'event' => 'presence:member_removed', - 'channel' => $this->getName(), - 'data' => [ - 'socket' => $connection->socketId, - 'total_count' => collect($connections) - ->filter(fn($conn) => ($conn->remoteAddress && $conn->remoteAddress != '127.0.0.1')) - ->count(), - ], - ]; - - if ($connection->remoteAddress && $connection->remoteAddress != '127.0.0.1') { - $this->broadcastToEveryoneExcept( - (object) $memberRemovedPayload, - $connection->socketId, - $connection->app->id, - false - ); - } + $this->informPresence($connection, $connections, true); }) ->then(function () use ($truth) { return $truth; }); } + + public function informPresence( + $connection, + $connections, + bool $isLeaving = false + ) { + $memberAddedPayload = [ + 'event' => 'presence.changed', + 'channel' => $this->getName(), + 'data' => [ + ($isLeaving ? 'removed' : 'joined') => $connection->socketId, + 'total_count' => collect($connections) + ->filter(fn($conn) => ($conn->remoteAddress && $conn->remoteAddress != '127.0.0.1')) + ->count(), + 'sockets' => collect($connections) + ->filter(fn($conn) => ($conn->remoteAddress && $conn->remoteAddress != '127.0.0.1')) + ->pluck('socketId')->toArray(), + ], + ]; + + + if ($connection->remoteAddress && $connection->remoteAddress != '127.0.0.1') { + $this->broadcastToEveryoneExcept( + (object) $memberAddedPayload, + $connection->socketId, + $connection->app->id, + false + ); + } + } }