laravel-websockets/src/LaravelEcho/Pusher/Channels/PresenceChannel.php

54 lines
1.5 KiB
PHP
Raw Normal View History

2018-11-21 11:13:40 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels;
use Ratchet\ConnectionInterface;
class PresenceChannel extends Channel
{
protected $subscriptions = [];
2018-11-21 11:14:45 +00:00
/**
* @link https://pusher.com/docs/pusher_protocol#presence-channel-events
*
* @param ConnectionInterface $conn
* @param $payload
*/
2018-11-21 11:13:40 +00:00
public function subscribe(ConnectionInterface $conn, $payload)
{
2018-11-21 14:42:04 +00:00
$this->saveConnection($conn);
2018-11-21 11:13:40 +00:00
$channelData = json_decode($payload->channel_data);
$this->subscriptions[$channelData->user_id] = $channelData;
// Send the success event
$conn->send(json_encode([
'event' => 'pusher_internal:subscription_succeeded',
'channel' => $this->channelId,
'data' => json_encode($this->getChannelData())
]));
2018-11-21 20:23:25 +00:00
//TODO: send member_added message back to client, and broadcast to everyone on channel
2018-11-21 11:13:40 +00:00
}
2018-11-21 21:36:50 +00:00
public function unsubscribe(ConnectionInterface $connection)
{
parent::unsubscribe($connection);
//TODO: send member_removed message back to client, and broadcast to everyone on channel
}
2018-11-21 11:14:45 +00:00
/**
* @return array
*/
2018-11-21 11:13:40 +00:00
protected function getChannelData()
{
return [
'presence' => [
'ids' => array_keys($this->subscriptions),
'hash' => array_map(function($channelData) { return $channelData->user_info; }, $this->subscriptions),
'count' => count($this->subscriptions)
]
];
}
}