1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\Server\Logger; |
4
|
|
|
|
5
|
|
|
use BeyondCode\LaravelWebSockets\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
|
|
|
/** @var \Ratchet\Http\HttpServerInterface */ |
14
|
|
|
protected $app; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* WebsocketsLogger polymorphic constructor for Logger Singleton instanciation and WSServer instanciation. |
18
|
|
|
* |
19
|
|
|
* @param MessageComponentInterface|OutputInterface $app |
20
|
|
|
*/ |
21
|
|
|
public function __construct($app) |
22
|
|
|
{ |
23
|
|
|
if ($app instanceof OutputInterface) { |
|
|
|
|
24
|
|
|
parent::__construct($app); |
25
|
|
|
} else { |
26
|
|
|
$this->app = $app; |
|
|
|
|
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function decorate(MessageComponentInterface $app): self |
31
|
|
|
{ |
32
|
|
|
$logger = app(self::class); |
33
|
|
|
|
34
|
|
|
return $logger->setApp($app); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function setApp(MessageComponentInterface $app) |
38
|
|
|
{ |
39
|
|
|
$this->app = $app; |
|
|
|
|
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function onOpen(ConnectionInterface $connection) |
45
|
|
|
{ |
46
|
|
|
$appKey = QueryParameters::create($connection->httpRequest)->get('appKey'); |
|
|
|
|
47
|
|
|
|
48
|
|
|
$this->warn("New connection opened for app key {$appKey}."); |
49
|
|
|
|
50
|
|
|
$this->app->onOpen(ConnectionLogger::decorate($connection)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function onMessage(ConnectionInterface $connection, MessageInterface $message) |
54
|
|
|
{ |
55
|
|
|
$this->info("{$connection->app->id}: connection id {$connection->socketId} received message: {$message->getPayload()}."); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$this->app->onMessage(ConnectionLogger::decorate($connection), $message); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function onClose(ConnectionInterface $connection) |
61
|
|
|
{ |
62
|
|
|
$socketId = $connection->socketId ?? null; |
|
|
|
|
63
|
|
|
|
64
|
|
|
$this->warn("Connection id {$socketId} closed."); |
65
|
|
|
|
66
|
|
|
$this->app->onClose(ConnectionLogger::decorate($connection)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function onError(ConnectionInterface $connection, Exception $exception) |
70
|
|
|
{ |
71
|
|
|
$exceptionClass = get_class($exception); |
72
|
|
|
|
73
|
|
|
$appId = $connection->app->id ?? 'Unknown app id'; |
|
|
|
|
74
|
|
|
|
75
|
|
|
$message = "{$appId}: exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`."; |
76
|
|
|
|
77
|
|
|
if ($this->verbose) { |
78
|
|
|
$message .= $exception->getTraceAsString(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->error($message); |
82
|
|
|
|
83
|
|
|
$this->app->onError(ConnectionLogger::decorate($connection), $exception); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.