|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\Server\Loggers; |
|
4
|
|
|
|
|
5
|
|
|
use BeyondCode\LaravelWebSockets\Server\QueryParameters; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use Ratchet\ConnectionInterface; |
|
8
|
|
|
use Ratchet\RFC6455\Messaging\MessageInterface; |
|
9
|
|
|
use Ratchet\WebSocket\MessageComponentInterface; |
|
10
|
|
|
|
|
11
|
|
|
class WebSocketsLogger extends Logger implements MessageComponentInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The HTTP app instance to watch. |
|
15
|
|
|
* |
|
16
|
|
|
* @var \Ratchet\Http\HttpServerInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $app; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Create a new instance and add the app to watch. |
|
22
|
|
|
* |
|
23
|
|
|
* @param \Ratchet\MessageComponentInterface $app |
|
24
|
|
|
* @return self |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function decorate(MessageComponentInterface $app): self |
|
27
|
|
|
{ |
|
28
|
|
|
$logger = clone app(self::class); |
|
29
|
|
|
|
|
30
|
|
|
return $logger->setApp($app); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Set a new app to watch. |
|
35
|
|
|
* |
|
36
|
|
|
* @param \Ratchet\MessageComponentInterface $app |
|
37
|
|
|
* @return $this |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setApp(MessageComponentInterface $app) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->app = $app; |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Handle the HTTP open request. |
|
48
|
|
|
* |
|
49
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
|
|
public function onOpen(ConnectionInterface $connection) |
|
53
|
|
|
{ |
|
54
|
|
|
$appKey = QueryParameters::create($connection->httpRequest)->get('appKey'); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
$this->warn("New connection opened for app key {$appKey}."); |
|
57
|
|
|
|
|
58
|
|
|
$this->app->onOpen(ConnectionLogger::decorate($connection)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Handle the HTTP message request. |
|
63
|
|
|
* |
|
64
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
|
65
|
|
|
* @param \Ratchet\RFC6455\Messaging\MessageInterface $message |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
public function onMessage(ConnectionInterface $connection, MessageInterface $message) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->info("{$connection->app->id}: connection id {$connection->socketId} received message: {$message->getPayload()}."); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
$this->app->onMessage(ConnectionLogger::decorate($connection), $message); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Handle the HTTP close request. |
|
77
|
|
|
* |
|
78
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
|
79
|
|
|
* @return void |
|
80
|
|
|
*/ |
|
81
|
|
|
public function onClose(ConnectionInterface $connection) |
|
82
|
|
|
{ |
|
83
|
|
|
$socketId = $connection->socketId ?? null; |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
$this->warn("Connection id {$socketId} closed."); |
|
86
|
|
|
|
|
87
|
|
|
$this->app->onClose(ConnectionLogger::decorate($connection)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Handle HTTP errors. |
|
92
|
|
|
* |
|
93
|
|
|
* @param \Ratchet\ConnectionInterface $connection |
|
94
|
|
|
* @param Exception $exception |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
|
|
public function onError(ConnectionInterface $connection, Exception $exception) |
|
98
|
|
|
{ |
|
99
|
|
|
$exceptionClass = get_class($exception); |
|
100
|
|
|
|
|
101
|
|
|
$appId = $connection->app->id ?? 'Unknown app id'; |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
$message = "{$appId}: exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`."; |
|
104
|
|
|
|
|
105
|
|
|
if ($this->verbose) { |
|
106
|
|
|
$message .= $exception->getTraceAsString(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$this->error($message); |
|
110
|
|
|
|
|
111
|
|
|
$this->app->onError(ConnectionLogger::decorate($connection), $exception); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.