laravel-websockets/src/Dashboard/DashboardLogger.php

118 lines
3.7 KiB
PHP
Raw Normal View History

2018-11-25 21:24:31 +00:00
<?php
2018-11-27 15:15:22 +00:00
namespace BeyondCode\LaravelWebSockets\Dashboard;
2018-11-25 21:24:31 +00:00
2018-11-27 15:35:28 +00:00
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
2020-03-04 09:58:39 +00:00
use Ratchet\ConnectionInterface;
use stdClass;
2018-11-25 21:24:31 +00:00
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-';
2020-08-14 06:14:14 +00:00
2018-11-25 21:24:31 +00:00
const TYPE_DISCONNECTION = 'disconnection';
2020-08-14 06:14:14 +00:00
2018-11-25 21:24:31 +00:00
const TYPE_CONNECTION = 'connection';
2020-08-14 06:14:14 +00:00
2018-11-25 21:24:31 +00:00
const TYPE_VACATED = 'vacated';
2020-08-14 06:14:14 +00:00
2018-11-25 21:24:31 +00:00
const TYPE_OCCUPIED = 'occupied';
2020-08-14 06:14:14 +00:00
2018-11-25 21:24:31 +00:00
const TYPE_SUBSCRIBED = 'subscribed';
2020-08-14 06:14:14 +00:00
2018-11-25 23:49:36 +00:00
const TYPE_CLIENT_MESSAGE = 'client-message';
2020-08-14 06:14:14 +00:00
2018-11-25 23:49:36 +00:00
const TYPE_API_MESSAGE = 'api-message';
2018-11-25 21:24:31 +00:00
2020-08-14 06:14:14 +00:00
const TYPE_REPLICATOR_SUBSCRIBED = 'replicator-subscribed';
const TYPE_REPLICATOR_UNSUBSCRIBED = 'replicator-unsubscribed';
2018-11-25 21:24:31 +00:00
public static function connection(ConnectionInterface $connection)
{
/** @var \GuzzleHttp\Psr7\Request $request */
$request = $connection->httpRequest;
2018-12-01 13:12:15 +00:00
static::log($connection->app->id, static::TYPE_CONNECTION, [
2018-11-25 21:24:31 +00:00
'details' => "Origin: {$request->getUri()->getScheme()}://{$request->getUri()->getHost()}",
'socketId' => $connection->socketId,
]);
}
2018-12-01 11:26:08 +00:00
public static function occupied(ConnectionInterface $connection, string $channelName)
2018-11-25 21:24:31 +00:00
{
2018-12-01 13:12:15 +00:00
static::log($connection->app->id, static::TYPE_OCCUPIED, [
2018-12-01 11:26:08 +00:00
'details' => "Channel: {$channelName}",
2018-11-25 21:24:31 +00:00
]);
}
2018-12-01 11:26:08 +00:00
public static function subscribed(ConnectionInterface $connection, string $channelName)
2018-11-25 21:24:31 +00:00
{
2018-12-01 13:12:15 +00:00
static::log($connection->app->id, static::TYPE_SUBSCRIBED, [
2018-11-25 23:47:47 +00:00
'socketId' => $connection->socketId,
2018-12-01 11:26:08 +00:00
'details' => "Channel: {$channelName}",
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-12-01 13:12:15 +00:00
static::log($connection->app->id, static::TYPE_CLIENT_MESSAGE, [
2018-11-25 23:47:47 +00:00
'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-12-01 13:12:15 +00:00
static::log($connection->app->id, static::TYPE_DISCONNECTION, [
2018-11-25 21:24:31 +00:00
'socketId' => $connection->socketId,
]);
}
2018-12-01 11:26:08 +00:00
public static function vacated(ConnectionInterface $connection, string $channelName)
2018-11-25 21:24:31 +00:00
{
2018-12-01 13:12:15 +00:00
static::log($connection->app->id, static::TYPE_VACATED, [
2018-12-01 11:26:08 +00:00
'details' => "Channel: {$channelName}",
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
]);
}
2020-08-14 06:14:14 +00:00
public static function replicatorSubscribed(string $appId, string $channel, string $serverId)
{
static::log($appId, static::TYPE_REPLICATOR_SUBSCRIBED, [
'details' => "Server ID: {$serverId} on Channel: {$channel}",
]);
}
public static function replicatorUnsubscribed(string $appId, string $channel, string $serverId)
{
static::log($appId, static::TYPE_REPLICATOR_UNSUBSCRIBED, [
'details' => "Server ID: {$serverId} on Channel: {$channel}",
]);
}
2018-11-25 21:24:31 +00:00
public static function log($appId, string $type, array $attributes = [])
{
2018-12-04 21:22:33 +00:00
$channelName = static::LOG_CHANNEL_PREFIX.$type;
2018-11-25 21:24:31 +00:00
2018-12-01 11:26:08 +00:00
$channel = app(ChannelManager::class)->find($appId, $channelName);
2018-11-25 21:24:31 +00:00
optional($channel)->broadcast([
2018-11-25 23:49:36 +00:00
'event' => 'log-message',
2018-12-01 11:26:08 +00:00
'channel' => $channelName,
2018-11-25 21:24:31 +00:00
'data' => [
'type' => $type,
2018-12-04 21:22:33 +00:00
'time' => strftime('%H:%M:%S'),
2018-11-25 23:47:47 +00:00
] + $attributes,
2018-11-25 21:24:31 +00:00
]);
}
2018-12-04 21:22:33 +00:00
}