1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\Dashboard; |
4
|
|
|
|
5
|
|
|
use stdClass; |
6
|
|
|
use Ratchet\ConnectionInterface; |
7
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; |
8
|
|
|
|
9
|
|
|
class DashboardLogger |
10
|
|
|
{ |
11
|
|
|
const LOG_CHANNEL_PREFIX = 'private-websockets-dashboard-'; |
12
|
|
|
const TYPE_DISCONNECTION = 'disconnection'; |
13
|
|
|
const TYPE_CONNECTION = 'connection'; |
14
|
|
|
const TYPE_VACATED = 'vacated'; |
15
|
|
|
const TYPE_OCCUPIED = 'occupied'; |
16
|
|
|
const TYPE_SUBSCRIBED = 'subscribed'; |
17
|
|
|
const TYPE_CLIENT_MESSAGE = 'client-message'; |
18
|
|
|
const TYPE_API_MESSAGE = 'api-message'; |
19
|
|
|
|
20
|
|
|
public static function connection(ConnectionInterface $connection) |
21
|
|
|
{ |
22
|
|
|
/** @var \GuzzleHttp\Psr7\Request $request */ |
23
|
|
|
$request = $connection->httpRequest; |
|
|
|
|
24
|
|
|
|
25
|
|
|
static::log($connection->app->id, static::TYPE_CONNECTION, [ |
|
|
|
|
26
|
|
|
'details' => "Origin: {$request->getUri()->getScheme()}://{$request->getUri()->getHost()}", |
27
|
|
|
'socketId' => $connection->socketId, |
|
|
|
|
28
|
|
|
]); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function occupied(ConnectionInterface $connection, string $channelName) |
32
|
|
|
{ |
33
|
|
|
static::log($connection->app->id, static::TYPE_OCCUPIED, [ |
|
|
|
|
34
|
|
|
'details' => "Channel: {$channelName}", |
35
|
|
|
]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function subscribed(ConnectionInterface $connection, string $channelName) |
39
|
|
|
{ |
40
|
|
|
static::log($connection->app->id, static::TYPE_SUBSCRIBED, [ |
|
|
|
|
41
|
|
|
'socketId' => $connection->socketId, |
|
|
|
|
42
|
|
|
'details' => "Channel: {$channelName}", |
43
|
|
|
]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function clientMessage(ConnectionInterface $connection, stdClass $payload) |
47
|
|
|
{ |
48
|
|
|
static::log($connection->app->id, static::TYPE_CLIENT_MESSAGE, [ |
|
|
|
|
49
|
|
|
'details' => "Channel: {$payload->channel}, Event: {$payload->event}", |
50
|
|
|
'socketId' => $connection->socketId, |
|
|
|
|
51
|
|
|
'data' => json_encode($payload), |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function disconnection(ConnectionInterface $connection) |
56
|
|
|
{ |
57
|
|
|
static::log($connection->app->id, static::TYPE_DISCONNECTION, [ |
|
|
|
|
58
|
|
|
'socketId' => $connection->socketId, |
|
|
|
|
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function vacated(ConnectionInterface $connection, string $channelName) |
63
|
|
|
{ |
64
|
|
|
static::log($connection->app->id, static::TYPE_VACATED, [ |
|
|
|
|
65
|
|
|
'details' => "Channel: {$channelName}", |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function apiMessage($appId, string $channel, string $event, string $payload) |
70
|
|
|
{ |
71
|
|
|
static::log($appId, static::TYPE_API_MESSAGE, [ |
72
|
|
|
'details' => "Channel: {$channel}, Event: {$event}", |
73
|
|
|
'data' => $payload, |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function log($appId, string $type, array $attributes = []) |
78
|
|
|
{ |
79
|
|
|
$channelName = static::LOG_CHANNEL_PREFIX.$type; |
80
|
|
|
|
81
|
|
|
$channel = app(ChannelManager::class)->find($appId, $channelName); |
82
|
|
|
|
83
|
|
|
optional($channel)->broadcast([ |
84
|
|
|
'event' => 'log-message', |
85
|
|
|
'channel' => $channelName, |
86
|
|
|
'data' => [ |
87
|
|
|
'type' => $type, |
88
|
|
|
'time' => strftime('%H:%M:%S'), |
89
|
|
|
] + $attributes, |
90
|
|
|
]); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: