laravel-websockets/src/Server/Messages/PusherClientMessage.php

81 lines
2.0 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
2020-09-10 19:59:26 +00:00
use BeyondCode\LaravelWebSockets\DashboardLogger;
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager;
2020-03-04 09:58:39 +00:00
use Illuminate\Support\Str;
use Ratchet\ConnectionInterface;
use stdClass;
2020-09-10 19:59:26 +00:00
use BeyondCode\LaravelWebSockets\Contracts\PusherMessage;
2018-11-21 22:47:46 +00:00
2018-12-01 14:09:05 +00:00
class PusherClientMessage implements PusherMessage
2018-11-21 22:47:46 +00:00
{
2020-08-18 17:21:22 +00:00
/**
* The payload to send.
*
* @var \stdClass
*/
2018-11-21 22:47:46 +00:00
protected $payload;
2020-08-18 17:21:22 +00:00
/**
* The socket connection.
*
* @var \Ratchet\ConnectionInterface
*/
2018-11-21 22:47:46 +00:00
protected $connection;
2020-08-18 17:21:22 +00:00
/**
* The channel manager.
*
* @var ChannelManager
*/
2018-11-21 22:47:46 +00:00
protected $channelManager;
2020-08-18 17:21:22 +00:00
/**
* Create a new instance.
*
* @param \stdClass $payload
* @param \Ratchet\ConnectionInterface $connection
* @param ChannelManager $channelManager
*/
2018-11-21 22:47:46 +00:00
public function __construct(stdClass $payload, ConnectionInterface $connection, ChannelManager $channelManager)
{
$this->payload = $payload;
$this->connection = $connection;
$this->channelManager = $channelManager;
}
2020-08-18 17:21:22 +00:00
/**
* Respond to the message construction.
*
* @return void
*/
2018-11-21 22:47:46 +00:00
public function respond()
{
if (! Str::startsWith($this->payload->event, 'client-')) {
2018-11-26 23:13:22 +00:00
return;
}
2018-11-25 21:24:31 +00:00
2018-12-01 14:59:46 +00:00
if (! $this->connection->app->clientMessagesEnabled) {
return;
}
2020-09-10 19:59:26 +00:00
$channel = $this->channelManager->find(
$this->connection->app->id, $this->payload->channel
);
optional($channel)->broadcastToEveryoneExcept(
$this->payload, $this->connection->socketId, $this->connection->app->id
);
2020-08-17 18:06:51 +00:00
DashboardLogger::log($this->connection->app->id, DashboardLogger::TYPE_WS_MESSAGE, [
'socketId' => $this->connection->socketId,
'channel' => $this->payload->channel,
'event' => $this->payload->event,
'data' => $this->payload,
]);
2018-11-21 22:47:46 +00:00
}
2018-12-04 21:22:33 +00:00
}