laravel-websockets/src/Server/Messages/PusherChannelProtocolMessag...

64 lines
1.7 KiB
PHP
Raw Normal View History

2018-11-21 22:47:46 +00:00
<?php
2020-09-10 19:59:26 +00:00
namespace BeyondCode\LaravelWebSockets\Server\Messages;
2018-11-21 22:47:46 +00:00
use Illuminate\Support\Str;
2018-12-04 21:22:33 +00:00
use Ratchet\ConnectionInterface;
2020-03-04 09:58:39 +00:00
use stdClass;
2018-11-21 22:47:46 +00:00
2020-09-10 19:59:26 +00:00
class PusherChannelProtocolMessage extends PusherClientMessage
2018-11-21 22:47:46 +00:00
{
2020-08-18 17:21:22 +00:00
/**
* Respond with the payload.
*
* @return void
*/
2018-11-21 22:47:46 +00:00
public function respond()
{
$eventName = Str::camel(Str::after($this->payload->event, ':'));
2018-11-21 22:47:46 +00:00
if (method_exists($this, $eventName) && $eventName !== 'respond') {
2018-11-25 23:14:50 +00:00
call_user_func([$this, $eventName], $this->connection, $this->payload->data ?? new stdClass());
2018-11-21 22:47:46 +00:00
}
}
2020-08-18 17:21:22 +00:00
/**
* Ping the connection.
*
* @see https://pusher.com/docs/pusher_protocol#ping-pong
* @param \Ratchet\ConnectionInterface $connection
* @return void
2018-11-21 22:47:46 +00:00
*/
2018-11-21 23:15:39 +00:00
protected function ping(ConnectionInterface $connection)
2018-11-21 22:47:46 +00:00
{
$connection->send(json_encode([
'event' => 'pusher:pong',
]));
}
2020-08-18 17:21:22 +00:00
/**
* Subscribe to channel.
*
* @see https://pusher.com/docs/pusher_protocol#pusher-subscribe
* @param \Ratchet\ConnectionInterface $connection
* @param \stdClass $payload
* @return void
2018-11-21 22:47:46 +00:00
*/
2018-11-21 23:15:39 +00:00
protected function subscribe(ConnectionInterface $connection, stdClass $payload)
2018-11-21 22:47:46 +00:00
{
2020-09-10 19:59:26 +00:00
$this->channelManager->subscribeToChannel($connection, $payload->channel, $payload);
2018-11-21 22:47:46 +00:00
}
2020-08-18 17:21:22 +00:00
/**
* Unsubscribe from the channel.
*
* @param \Ratchet\ConnectionInterface $connection
* @param \stdClass $payload
* @return void
*/
2018-11-21 22:47:46 +00:00
public function unsubscribe(ConnectionInterface $connection, stdClass $payload)
{
2020-09-10 19:59:26 +00:00
$this->channelManager->unsubscribeFromChannel($connection, $payload->channel, $payload);
2018-11-21 22:47:46 +00:00
}
}