laravel-websockets/src/DashboardLogger.php

98 lines
2.6 KiB
PHP
Raw Normal View History

2018-11-25 21:24:31 +00:00
<?php
2020-09-10 19:59:26 +00:00
namespace BeyondCode\LaravelWebSockets;
2018-11-25 21:24:31 +00:00
2020-09-10 19:59:26 +00:00
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager;
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
2020-08-17 18:06:51 +00:00
const TYPE_DISCONNECTED = 'disconnected';
2020-08-14 06:14:14 +00:00
2020-08-17 18:06:51 +00:00
const TYPE_CONNECTED = 'connected';
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
2020-08-17 18:06:51 +00:00
const TYPE_WS_MESSAGE = 'ws-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';
2020-08-17 18:06:51 +00:00
const TYPE_REPLICATOR_JOINED_CHANNEL = 'replicator-joined';
const TYPE_REPLICATOR_LEFT_CHANNEL = 'replicator-left';
const TYPE_REPLICATOR_MESSAGE_PUBLISHED = 'replicator-message-published';
const TYPE_REPLICATOR_MESSAGE_RECEIVED = 'replicator-message-received';
/**
* The list of all channels.
*
* @var array
*/
public static $channels = [
self::TYPE_DISCONNECTED,
self::TYPE_CONNECTED,
self::TYPE_OCCUPIED,
self::TYPE_SUBSCRIBED,
self::TYPE_WS_MESSAGE,
self::TYPE_API_MESSAGE,
self::TYPE_REPLICATOR_SUBSCRIBED,
self::TYPE_REPLICATOR_UNSUBSCRIBED,
self::TYPE_REPLICATOR_JOINED_CHANNEL,
self::TYPE_REPLICATOR_LEFT_CHANNEL,
self::TYPE_REPLICATOR_MESSAGE_PUBLISHED,
self::TYPE_REPLICATOR_MESSAGE_RECEIVED,
];
2020-08-18 17:21:22 +00:00
/**
* Log an event for an app.
*
* @param mixed $appId
* @param string $type
* @param array $details
* @return void
*/
2020-08-17 18:06:51 +00:00
public static function log($appId, string $type, array $details = [])
2018-11-25 21:24:31 +00:00
{
2020-09-10 19:59:26 +00:00
$channelManager = app(ChannelManager::class);
2018-11-25 21:24:31 +00:00
2020-09-10 19:59:26 +00:00
$channelName = static::LOG_CHANNEL_PREFIX.$type;
2018-11-25 21:24:31 +00:00
2020-09-10 19:59:26 +00:00
$payload = [
2018-12-01 11:26:08 +00:00
'channel' => $channelName,
2020-09-10 19:59:26 +00:00
'event' => 'log-message',
2018-11-25 21:24:31 +00:00
'data' => [
'type' => $type,
2018-12-04 21:22:33 +00:00
'time' => strftime('%H:%M:%S'),
2020-08-17 18:06:51 +00:00
'details' => $details,
],
2020-09-10 19:59:26 +00:00
];
// Here you can use the ->find(), even if the channel
// does not exist on the server. If it does not exist,
// then the message simply will get broadcasted
// across the other servers.
$channel = $channelManager->find($appId, $channelName);
if ($channel) {
$channel->broadcastToEveryoneExcept(
(object) $payload,
null,
$appId
);
} else {
$channelManager->broadcastAcrossServers(
$appId, null, $channelName, (object) $payload
2020-09-10 19:59:26 +00:00
);
}
2018-11-25 21:24:31 +00:00
}
2018-12-04 21:22:33 +00:00
}