laravel-websockets/src/WebSockets/Channels/PresenceChannel.php

100 lines
2.5 KiB
PHP
Raw Normal View History

2018-11-21 11:13:40 +00:00
<?php
2018-11-27 15:21:31 +00:00
namespace BeyondCode\LaravelWebSockets\WebSockets\Channels;
2018-11-21 11:13:40 +00:00
2018-12-04 21:22:33 +00:00
use Ratchet\ConnectionInterface;
2020-03-04 09:58:39 +00:00
use stdClass;
2018-11-21 11:13:40 +00:00
class PresenceChannel extends Channel
{
2018-11-22 21:02:36 +00:00
protected $users = [];
2018-11-21 11:13:40 +00:00
2018-11-22 21:09:32 +00:00
public function getUsers(): array
{
return $this->users;
}
2018-11-21 23:14:56 +00:00
/*
2018-11-21 11:14:45 +00:00
* @link https://pusher.com/docs/pusher_protocol#presence-channel-events
*/
2018-11-26 23:07:35 +00:00
public function subscribe(ConnectionInterface $connection, stdClass $payload)
2018-11-21 11:13:40 +00:00
{
2018-11-22 09:54:51 +00:00
$this->verifySignature($connection, $payload);
2018-11-21 23:03:13 +00:00
$this->saveConnection($connection);
2018-11-21 14:42:04 +00:00
2018-11-21 11:13:40 +00:00
$channelData = json_decode($payload->channel_data);
2018-11-27 15:55:30 +00:00
$this->users[$connection->socketId] = $channelData;
2018-11-21 11:13:40 +00:00
// Send the success event
2018-11-21 23:03:13 +00:00
$connection->send(json_encode([
2018-11-21 11:13:40 +00:00
'event' => 'pusher_internal:subscription_succeeded',
2018-12-01 11:41:58 +00:00
'channel' => $this->channelName,
2018-12-04 21:22:33 +00:00
'data' => json_encode($this->getChannelData()),
2018-11-21 11:13:40 +00:00
]));
2018-11-21 20:23:25 +00:00
2018-11-21 23:03:13 +00:00
$this->broadcastToOthers($connection, [
'event' => 'pusher_internal:member_added',
2018-12-01 11:41:58 +00:00
'channel' => $this->channelName,
2018-12-04 21:22:33 +00:00
'data' => json_encode($channelData),
2018-11-21 23:03:13 +00:00
]);
2018-11-21 11:13:40 +00:00
}
2018-11-21 21:36:50 +00:00
public function unsubscribe(ConnectionInterface $connection)
{
parent::unsubscribe($connection);
2018-12-03 21:11:44 +00:00
if (! isset($this->users[$connection->socketId])) {
return;
}
2018-11-22 07:37:47 +00:00
$this->broadcastToOthers($connection, [
'event' => 'pusher_internal:member_removed',
2018-12-01 11:41:58 +00:00
'channel' => $this->channelName,
2018-11-22 07:37:47 +00:00
'data' => json_encode([
2018-12-04 21:22:33 +00:00
'user_id' => $this->users[$connection->socketId]->user_id,
]),
2018-11-22 07:37:47 +00:00
]);
2018-11-22 21:02:36 +00:00
unset($this->users[$connection->socketId]);
2018-11-21 21:36:50 +00:00
}
2018-11-21 23:14:56 +00:00
protected function getChannelData(): array
2018-11-21 11:13:40 +00:00
{
return [
'presence' => [
2018-11-28 20:20:07 +00:00
'ids' => $this->getUserIds(),
'hash' => $this->getHash(),
'count' => count($this->users),
],
2018-11-21 11:13:40 +00:00
];
}
2018-11-22 21:02:36 +00:00
public function toArray(): array
{
2018-11-28 20:20:07 +00:00
return array_merge(parent::toArray(), [
2018-11-22 21:02:36 +00:00
'user_count' => count($this->users),
]);
}
2018-11-28 20:20:07 +00:00
protected function getUserIds(): array
{
$userIds = array_map(function ($channelData) {
2018-12-04 21:22:33 +00:00
return (string) $channelData->user_id;
2018-11-28 20:20:07 +00:00
}, $this->users);
return array_values($userIds);
}
protected function getHash(): array
{
$hash = [];
foreach ($this->users as $socketId => $channelData) {
$hash[$channelData->user_id] = $channelData->user_info;
}
return $hash;
}
2018-12-04 21:22:33 +00:00
}