1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\Server\Logger; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Ratchet\ConnectionInterface; |
7
|
|
|
use Ratchet\RFC6455\Messaging\MessageInterface; |
8
|
|
|
use Ratchet\WebSocket\MessageComponentInterface; |
9
|
|
|
use BeyondCode\LaravelWebSockets\QueryParameters; |
10
|
|
|
|
11
|
|
|
class WebsocketsLogger extends Logger implements MessageComponentInterface |
12
|
|
|
{ |
13
|
|
|
/** @var \Ratchet\Http\HttpServerInterface */ |
14
|
|
|
protected $app; |
15
|
|
|
|
16
|
|
|
public static function decorate(MessageComponentInterface $app): self |
17
|
|
|
{ |
18
|
|
|
$logger = app(self::class); |
19
|
|
|
|
20
|
|
|
return $logger->setApp($app); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function setApp(MessageComponentInterface $app) |
24
|
|
|
{ |
25
|
|
|
$this->app = $app; |
|
|
|
|
26
|
|
|
|
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function onOpen(ConnectionInterface $connection) |
31
|
|
|
{ |
32
|
|
|
$appKey = QueryParameters::create($connection->httpRequest)->get('appKey'); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$this->warn("New connection opened for app key {$appKey}."); |
35
|
|
|
|
36
|
|
|
$this->app->onOpen(ConnectionLogger::decorate($connection)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function onMessage(ConnectionInterface $connection, MessageInterface $message) |
40
|
|
|
{ |
41
|
|
|
$this->info("{$connection->app->id}: connection id {$connection->socketId} received message: {$message->getPayload()}."); |
|
|
|
|
42
|
|
|
|
43
|
|
|
$this->app->onMessage(ConnectionLogger::decorate($connection), $message); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function onClose(ConnectionInterface $connection) |
47
|
|
|
{ |
48
|
|
|
$socketId = $connection->socketId ?? null; |
|
|
|
|
49
|
|
|
|
50
|
|
|
$this->warn("Connection id {$socketId} closed."); |
51
|
|
|
|
52
|
|
|
$this->app->onClose(ConnectionLogger::decorate($connection)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function onError(ConnectionInterface $connection, Exception $exception) |
56
|
|
|
{ |
57
|
|
|
$exceptionClass = get_class($exception); |
58
|
|
|
|
59
|
|
|
$appId = $connection->app->id ?? 'Unknown app id'; |
|
|
|
|
60
|
|
|
|
61
|
|
|
$message = "{$appId}: exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`."; |
62
|
|
|
|
63
|
|
|
if ($this->verbose) { |
64
|
|
|
$message .= $exception->getTraceAsString(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->error($message); |
68
|
|
|
|
69
|
|
|
$this->app->onError(ConnectionLogger::decorate($connection), $exception); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..