laravel-websockets/src/WebSockets/Pusher/DashboardLogger.php

93 lines
3.0 KiB
PHP
Raw Normal View History

2018-11-25 21:24:31 +00:00
<?php
2018-11-27 15:11:12 +00:00
namespace BeyondCode\LaravelWebSockets\WebSockets\Pusher;
2018-11-25 21:24:31 +00:00
use Ratchet\ConnectionInterface;
2018-11-27 15:07:29 +00:00
use BeyondCode\LaravelWebSockets\WebSocket\Pusher\Channels\ChannelManager;
2018-11-25 21:24:31 +00:00
use stdClass;
2018-11-27 14:59:40 +00:00
class DashboardLogger
2018-11-25 21:24:31 +00:00
{
const LOG_CHANNEL_PREFIX = 'private-websockets-dashboard-';
const TYPE_DISCONNECTION = 'disconnection';
const TYPE_CONNECTION = 'connection';
const TYPE_VACATED = 'vacated';
const TYPE_OCCUPIED = 'occupied';
const TYPE_SUBSCRIBED = 'subscribed';
2018-11-25 23:49:36 +00:00
const TYPE_CLIENT_MESSAGE = 'client-message';
const TYPE_API_MESSAGE = 'api-message';
2018-11-25 21:24:31 +00:00
public static function connection(ConnectionInterface $connection)
{
/** @var \GuzzleHttp\Psr7\Request $request */
$request = $connection->httpRequest;
2018-11-25 23:47:47 +00:00
static::log($connection->client->appId, static::TYPE_CONNECTION, [
2018-11-25 21:24:31 +00:00
'details' => "Origin: {$request->getUri()->getScheme()}://{$request->getUri()->getHost()}",
'socketId' => $connection->socketId,
]);
}
2018-11-25 23:47:47 +00:00
public static function occupied(ConnectionInterface $connection, string $channelId)
2018-11-25 21:24:31 +00:00
{
2018-11-25 23:47:47 +00:00
static::log($connection->client->appId, static::TYPE_OCCUPIED, [
'details' => "Channel: {$channelId}",
2018-11-25 21:24:31 +00:00
]);
}
2018-11-25 23:47:47 +00:00
public static function subscribed(ConnectionInterface $connection, string $channelId)
2018-11-25 21:24:31 +00:00
{
2018-11-25 23:47:47 +00:00
static::log($connection->client->appId, static::TYPE_SUBSCRIBED, [
'socketId' => $connection->socketId,
'details' => "Channel: {$channelId}",
2018-11-25 21:24:31 +00:00
]);
}
2018-11-25 23:47:47 +00:00
public static function clientMessage(ConnectionInterface $connection, stdClass $payload)
2018-11-25 21:24:31 +00:00
{
2018-11-25 23:47:47 +00:00
static::log($connection->client->appId, static::TYPE_CLIENT_MESSAGE, [
'details' => "Channel: {$payload->channel}, Event: {$payload->event}",
'socketId' => $connection->socketId,
'data' => json_encode($payload),
2018-11-25 21:24:31 +00:00
]);
}
2018-11-25 23:47:47 +00:00
public static function disconnection(ConnectionInterface $connection)
2018-11-25 21:24:31 +00:00
{
2018-11-25 23:47:47 +00:00
static::log($connection->client->appId, static::TYPE_DISCONNECTION, [
2018-11-25 21:24:31 +00:00
'socketId' => $connection->socketId,
]);
}
2018-11-25 23:47:47 +00:00
public static function vacated(ConnectionInterface $connection, string $channelId)
2018-11-25 21:24:31 +00:00
{
2018-11-25 23:47:47 +00:00
static::log($connection->client->appId, static::TYPE_VACATED, [
'details' => "Channel: {$channelId}",
2018-11-25 21:24:31 +00:00
]);
}
public static function apiMessage($appId, string $channel, string $event, string $payload)
{
2018-11-25 23:47:47 +00:00
static::log($appId, static::TYPE_API_MESSAGE, [
2018-11-25 21:24:31 +00:00
'details' => "Channel: {$channel}, Event: {$event}",
2018-11-25 23:47:47 +00:00
'data' => $payload,
2018-11-25 21:24:31 +00:00
]);
}
public static function log($appId, string $type, array $attributes = [])
{
2018-11-25 23:47:47 +00:00
$channelId = static::LOG_CHANNEL_PREFIX . $type;
2018-11-25 21:24:31 +00:00
$channel = app(ChannelManager::class)->find($appId, $channelId);
optional($channel)->broadcast([
2018-11-25 23:49:36 +00:00
'event' => 'log-message',
2018-11-25 21:24:31 +00:00
'channel' => $channelId,
'data' => [
'type' => $type,
'time' => strftime("%H:%M:%S")
2018-11-25 23:47:47 +00:00
] + $attributes,
2018-11-25 21:24:31 +00:00
]);
}
}