diff --git a/src/Channels/OpenPresenceChannel.php b/src/Channels/OpenPresenceChannel.php index 315faee..ef3f42e 100644 --- a/src/Channels/OpenPresenceChannel.php +++ b/src/Channels/OpenPresenceChannel.php @@ -54,11 +54,16 @@ class OpenPresenceChannel extends Channel */ public function unsubscribe(ConnectionInterface $connection): PromiseInterface { + \Log::channel('websocket')->info('Presence unsub'); + $truth = parent::unsubscribe($connection); + \Log::channel('websocket')->info('Presence unsub 1'); + return $this->channelManager ->getLocalConnections() ->then(function ($connections) use ($connection) { + \Log::channel('websocket')->info('Presence unsub 2 local conn'); $this->informPresence($connection, $connections, true); }) ->then(function () use ($truth) { @@ -78,13 +83,17 @@ class OpenPresenceChannel extends Channel ($isLeaving ? 'removed' : 'joined') => $connection->socketId, 'total_count' => collect($connections) ->filter(fn($conn) => ($conn->remoteAddress && $conn->remoteAddress != '127.0.0.1')) + ->filter(fn($conn) => $isLeaving ? $conn->socketId != $connection->socketId : true) ->count(), 'sockets' => collect($connections) ->filter(fn($conn) => ($conn->remoteAddress && $conn->remoteAddress != '127.0.0.1')) + ->filter(fn($conn) => $isLeaving ? $conn->socketId != $connection->socketId : true) ->pluck('socketId')->toArray(), ], ]; + if (!$isLeaving) + $connection->send(json_encode($memberAddedPayload)); if ($connection->remoteAddress && $connection->remoteAddress != '127.0.0.1') { $this->broadcastToEveryoneExcept( diff --git a/src/Websocket/Handler.php b/src/Websocket/Handler.php index 438fa2c..33cea99 100644 --- a/src/Websocket/Handler.php +++ b/src/Websocket/Handler.php @@ -111,7 +111,7 @@ class Handler implements MessageComponentInterface $channel = $this->handleChannelSubscriptions($message, $connection); - if (! $channel->hasConnection($connection) && ( + if (! $channel->hasConnection($connection) && !( $message['event'] !== 'pusher:unsubscribe' && $message['event'] !== 'pusher.unsubscribe' )) { @@ -126,7 +126,7 @@ class Handler implements MessageComponentInterface $this->authenticateConnection($connection, $channel, $message); - \Log::channel('websocket')->info('['.$connection->socketId.']@'.$channel->getName().' | ' . json_encode($message)); + \Log::channel('websocket')->info('[' . $connection->socketId . ']@' . $channel->getName() . ' | ' . json_encode($message)); if (strpos($message['event'], 'pusher') !== false) { return $connection->send(json_encode([ @@ -374,13 +374,13 @@ class Handler implements MessageComponentInterface ); } - protected function handleChannelSubscriptions($message, $connection) : ?Channel + protected function handleChannelSubscriptions($message, $connection): ?Channel { $channel = $this->get_connection_channel($connection, $message); $channel_name = optional($channel)->getName(); $socket_id = $connection->socketId; - if(! $channel_name || ! $channel) { + if (! $channel_name || ! $channel) { return null; } @@ -408,7 +408,7 @@ class Handler implements MessageComponentInterface ); if (! $channel->hasConnection($connection)) { - try{ + try { $channel->subscribe($connection, (object) $message); } catch (\Throwable $e) { return null;