2018-11-24 14:23:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
namespace BeyondCode\LaravelWebSockets\Server\Loggers;
|
2018-11-24 14:23:59 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Server\QueryParameters;
|
2018-11-24 14:23:59 +00:00
|
|
|
use Exception;
|
|
|
|
|
use Ratchet\ConnectionInterface;
|
|
|
|
|
use Ratchet\RFC6455\Messaging\MessageInterface;
|
|
|
|
|
use Ratchet\WebSocket\MessageComponentInterface;
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
class WebSocketsLogger extends Logger implements MessageComponentInterface
|
2018-11-24 14:23:59 +00:00
|
|
|
{
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* The HTTP app instance to watch.
|
|
|
|
|
*
|
|
|
|
|
* @var \Ratchet\Http\HttpServerInterface
|
|
|
|
|
*/
|
2018-11-24 14:23:59 +00:00
|
|
|
protected $app;
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Create a new instance and add the app to watch.
|
|
|
|
|
*
|
|
|
|
|
* @param \Ratchet\MessageComponentInterface $app
|
2020-08-18 17:21:44 +00:00
|
|
|
* @return self
|
2020-08-18 17:21:22 +00:00
|
|
|
*/
|
2018-12-04 21:22:33 +00:00
|
|
|
public static function decorate(MessageComponentInterface $app): self
|
2018-11-24 14:23:59 +00:00
|
|
|
{
|
2020-07-04 13:51:24 +00:00
|
|
|
$logger = clone app(self::class);
|
2018-11-24 14:23:59 +00:00
|
|
|
|
|
|
|
|
return $logger->setApp($app);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Set a new app to watch.
|
|
|
|
|
*
|
|
|
|
|
* @param \Ratchet\MessageComponentInterface $app
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2018-11-24 14:23:59 +00:00
|
|
|
public function setApp(MessageComponentInterface $app)
|
|
|
|
|
{
|
|
|
|
|
$this->app = $app;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Handle the HTTP open request.
|
|
|
|
|
*
|
|
|
|
|
* @param \Ratchet\ConnectionInterface $connection
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-11-24 14:23:59 +00:00
|
|
|
public function onOpen(ConnectionInterface $connection)
|
|
|
|
|
{
|
2018-11-25 23:42:45 +00:00
|
|
|
$appKey = QueryParameters::create($connection->httpRequest)->get('appKey');
|
2018-11-24 14:23:59 +00:00
|
|
|
|
2018-11-25 23:42:45 +00:00
|
|
|
$this->warn("New connection opened for app key {$appKey}.");
|
2018-11-24 14:23:59 +00:00
|
|
|
|
|
|
|
|
$this->app->onOpen(ConnectionLogger::decorate($connection));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Handle the HTTP message request.
|
|
|
|
|
*
|
|
|
|
|
* @param \Ratchet\ConnectionInterface $connection
|
|
|
|
|
* @param \Ratchet\RFC6455\Messaging\MessageInterface $message
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-11-24 14:23:59 +00:00
|
|
|
public function onMessage(ConnectionInterface $connection, MessageInterface $message)
|
|
|
|
|
{
|
2018-12-01 13:12:15 +00:00
|
|
|
$this->info("{$connection->app->id}: connection id {$connection->socketId} received message: {$message->getPayload()}.");
|
2018-11-24 14:23:59 +00:00
|
|
|
|
|
|
|
|
$this->app->onMessage(ConnectionLogger::decorate($connection), $message);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Handle the HTTP close request.
|
|
|
|
|
*
|
|
|
|
|
* @param \Ratchet\ConnectionInterface $connection
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-11-24 14:23:59 +00:00
|
|
|
public function onClose(ConnectionInterface $connection)
|
|
|
|
|
{
|
2018-12-04 16:25:11 +00:00
|
|
|
$socketId = $connection->socketId ?? null;
|
|
|
|
|
|
|
|
|
|
$this->warn("Connection id {$socketId} closed.");
|
2018-11-24 14:23:59 +00:00
|
|
|
|
|
|
|
|
$this->app->onClose(ConnectionLogger::decorate($connection));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Handle HTTP errors.
|
|
|
|
|
*
|
|
|
|
|
* @param \Ratchet\ConnectionInterface $connection
|
|
|
|
|
* @param Exception $exception
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-11-24 14:23:59 +00:00
|
|
|
public function onError(ConnectionInterface $connection, Exception $exception)
|
|
|
|
|
{
|
|
|
|
|
$exceptionClass = get_class($exception);
|
|
|
|
|
|
2018-12-01 13:12:15 +00:00
|
|
|
$appId = $connection->app->id ?? 'Unknown app id';
|
2018-11-24 14:23:59 +00:00
|
|
|
|
2018-11-27 20:43:44 +00:00
|
|
|
$message = "{$appId}: exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`.";
|
2018-11-24 14:23:59 +00:00
|
|
|
|
|
|
|
|
if ($this->verbose) {
|
|
|
|
|
$message .= $exception->getTraceAsString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->error($message);
|
|
|
|
|
|
|
|
|
|
$this->app->onError(ConnectionLogger::decorate($connection), $exception);
|
|
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|