This commit is contained in:
Marcel Pociot 2018-11-22 08:37:47 +01:00
parent fa6313b215
commit 5d8b49b60d
2 changed files with 13 additions and 5 deletions

View File

@ -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));
}
}

View File

@ -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)
]