1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets; |
4
|
|
|
|
5
|
|
|
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager; |
6
|
|
|
|
7
|
|
|
class DashboardLogger |
8
|
|
|
{ |
9
|
|
|
const LOG_CHANNEL_PREFIX = 'private-websockets-dashboard-'; |
10
|
|
|
|
11
|
|
|
const TYPE_DISCONNECTED = 'disconnected'; |
12
|
|
|
|
13
|
|
|
const TYPE_CONNECTED = 'connected'; |
14
|
|
|
|
15
|
|
|
const TYPE_OCCUPIED = 'occupied'; |
16
|
|
|
|
17
|
|
|
const TYPE_SUBSCRIBED = 'subscribed'; |
18
|
|
|
|
19
|
|
|
const TYPE_WS_MESSAGE = 'ws-message'; |
20
|
|
|
|
21
|
|
|
const TYPE_API_MESSAGE = 'api-message'; |
22
|
|
|
|
23
|
|
|
const TYPE_REPLICATOR_SUBSCRIBED = 'replicator-subscribed'; |
24
|
|
|
|
25
|
|
|
const TYPE_REPLICATOR_UNSUBSCRIBED = 'replicator-unsubscribed'; |
26
|
|
|
|
27
|
|
|
const TYPE_REPLICATOR_JOINED_CHANNEL = 'replicator-joined'; |
28
|
|
|
|
29
|
|
|
const TYPE_REPLICATOR_LEFT_CHANNEL = 'replicator-left'; |
30
|
|
|
|
31
|
|
|
const TYPE_REPLICATOR_MESSAGE_PUBLISHED = 'replicator-message-published'; |
32
|
|
|
|
33
|
|
|
const TYPE_REPLICATOR_MESSAGE_RECEIVED = 'replicator-message-received'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The list of all channels. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
public static $channels = [ |
41
|
|
|
self::TYPE_DISCONNECTED, |
42
|
|
|
self::TYPE_CONNECTED, |
43
|
|
|
self::TYPE_OCCUPIED, |
44
|
|
|
self::TYPE_SUBSCRIBED, |
45
|
|
|
self::TYPE_WS_MESSAGE, |
46
|
|
|
self::TYPE_API_MESSAGE, |
47
|
|
|
self::TYPE_REPLICATOR_SUBSCRIBED, |
48
|
|
|
self::TYPE_REPLICATOR_UNSUBSCRIBED, |
49
|
|
|
self::TYPE_REPLICATOR_JOINED_CHANNEL, |
50
|
|
|
self::TYPE_REPLICATOR_LEFT_CHANNEL, |
51
|
|
|
self::TYPE_REPLICATOR_MESSAGE_PUBLISHED, |
52
|
|
|
self::TYPE_REPLICATOR_MESSAGE_RECEIVED, |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Log an event for an app. |
57
|
|
|
* |
58
|
|
|
* @param mixed $appId |
59
|
|
|
* @param string $type |
60
|
|
|
* @param array $details |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public static function log($appId, string $type, array $details = []) |
64
|
|
|
{ |
65
|
|
|
$channelManager = app(ChannelManager::class); |
66
|
|
|
|
67
|
|
|
$channelName = static::LOG_CHANNEL_PREFIX.$type; |
68
|
|
|
|
69
|
|
|
$payload = [ |
70
|
|
|
'channel' => $channelName, |
71
|
|
|
'event' => 'log-message', |
72
|
|
|
'data' => [ |
73
|
|
|
'type' => $type, |
74
|
|
|
'time' => strftime('%H:%M:%S'), |
75
|
|
|
'details' => $details, |
76
|
|
|
], |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
// Here you can use the ->find(), even if the channel |
80
|
|
|
// does not exist on the server. If it does not exist, |
81
|
|
|
// then the message simply will get broadcasted |
82
|
|
|
// across the other servers. |
83
|
|
|
$channel = $channelManager->find($appId, $channelName); |
84
|
|
|
|
85
|
|
|
if ($channel) { |
86
|
|
|
$channel->broadcastToEveryoneExcept( |
87
|
|
|
(object) $payload, |
88
|
|
|
null, |
89
|
|
|
$appId |
90
|
|
|
); |
91
|
|
|
} else { |
92
|
|
|
$channelManager->broadcastAcrossServers( |
93
|
|
|
$appId, $channelName, (object) $payload |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|