wip
This commit is contained in:
parent
f1bc50b0be
commit
d6ef5883a9
|
|
@ -3,7 +3,8 @@
|
||||||
namespace BeyondCode\LaravelWebSockets\Console;
|
namespace BeyondCode\LaravelWebSockets\Console;
|
||||||
|
|
||||||
use BeyondCode\LaravelWebSockets\Facades\WebSocketRouter;
|
use BeyondCode\LaravelWebSockets\Facades\WebSocketRouter;
|
||||||
use BeyondCode\LaravelWebSockets\Server\Logger;
|
use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger;
|
||||||
|
use BeyondCode\LaravelWebSockets\Server\Logger\MessageLogger;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use BeyondCode\LaravelWebSockets\Server\WebSocketServer;
|
use BeyondCode\LaravelWebSockets\Server\WebSocketServer;
|
||||||
|
|
||||||
|
|
@ -18,15 +19,28 @@ class StartWebSocketServer extends Command
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
$this
|
$this
|
||||||
->configureLogger()
|
->configureMessageLogger()
|
||||||
|
->configureConnectionLogger()
|
||||||
->registerEchoRoutes()
|
->registerEchoRoutes()
|
||||||
->startWebSocketServer();
|
->startWebSocketServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configureLogger()
|
protected function configureMessageLogger()
|
||||||
{
|
{
|
||||||
app()->singleton(Logger::class, function() {
|
app()->singleton(MessageLogger::class, function() {
|
||||||
return (new Logger($this->output))
|
return (new MessageLogger($this->output))
|
||||||
|
->enable(config('app.debug'))
|
||||||
|
//TODO: use real option
|
||||||
|
->verbose($this->hasOption('vvv'));
|
||||||
|
});
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configureConnectionLogger()
|
||||||
|
{
|
||||||
|
app()->bind(ConnectionLogger::class, function() {
|
||||||
|
return (new ConnectionLogger($this->output))
|
||||||
->enable(config('app.debug'))
|
->enable(config('app.debug'))
|
||||||
//TODO: use real option
|
//TODO: use real option
|
||||||
->verbose($this->hasOption('vvv'));
|
->verbose($this->hasOption('vvv'));
|
||||||
|
|
@ -49,7 +63,7 @@ class StartWebSocketServer extends Command
|
||||||
$routes = WebSocketRouter::getRoutes();
|
$routes = WebSocketRouter::getRoutes();
|
||||||
|
|
||||||
/** 🎩 Start the magic 🎩 */
|
/** 🎩 Start the magic 🎩 */
|
||||||
return (new WebSocketServer($routes))
|
(new WebSocketServer($routes))
|
||||||
->setHost($this->option('host'))
|
->setHost($this->option('host'))
|
||||||
->setPort($this->option('port'))
|
->setPort($this->option('port'))
|
||||||
->setConsoleOutput($this->output)
|
->setConsoleOutput($this->output)
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ abstract class EchoController implements HttpServerInterface
|
||||||
'error' => $exception->getMessage()
|
'error' => $exception->getMessage()
|
||||||
]));
|
]));
|
||||||
|
|
||||||
$connection->send(Psr\str($response));
|
$connection->send(gPsr\str($response));
|
||||||
$connection->close();
|
$connection->close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebsockets\LaravelEcho\Pusher\Exceptions;
|
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
|
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
|
||||||
|
|
||||||
use BeyondCode\LaravelWebSockets\ClientProviders\Client;
|
use BeyondCode\LaravelWebSockets\ClientProviders\Client;
|
||||||
use BeyondCode\LaravelWebsockets\LaravelEcho\Pusher\Exceptions\PusherException;
|
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\PusherException;
|
||||||
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\UnknownAppKey;
|
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\UnknownAppKey;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,6 @@ class RespondableMessageFactory
|
||||||
{
|
{
|
||||||
$payload = json_decode($message->getPayload());
|
$payload = json_decode($message->getPayload());
|
||||||
|
|
||||||
// Log this for now
|
|
||||||
dump($payload);
|
|
||||||
|
|
||||||
return starts_with($payload->event, 'pusher:')
|
return starts_with($payload->event, 'pusher:')
|
||||||
? new PusherMessage($payload, $connection, $channelManager)
|
? new PusherMessage($payload, $connection, $channelManager)
|
||||||
: new Message($payload, $connection, $channelManager);
|
: new Message($payload, $connection, $channelManager);
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets;
|
namespace BeyondCode\LaravelWebSockets;
|
||||||
|
|
||||||
use BeyondCode\LaravelWebSockets\Server\Logger;
|
use BeyondCode\LaravelWebSockets\Server\Logger\MessageLogger;
|
||||||
use Ratchet\WebSocket\WsServer;
|
use Ratchet\WebSocket\WsServer;
|
||||||
use Symfony\Component\Routing\Route;
|
use Symfony\Component\Routing\Route;
|
||||||
use Ratchet\Http\HttpServerInterface;
|
use Ratchet\Http\HttpServerInterface;
|
||||||
|
|
@ -83,8 +83,8 @@ class Router
|
||||||
if (is_subclass_of($action, WebSocketController::class)) {
|
if (is_subclass_of($action, WebSocketController::class)) {
|
||||||
$app = app($action);
|
$app = app($action);
|
||||||
|
|
||||||
if (Logger::isEnabled()) {
|
if (MessageLogger::isEnabled()) {
|
||||||
$app = Logger::decorate($app);
|
$app = MessageLogger::decorate($app);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new WsServer($app);
|
return new WsServer($app);
|
||||||
|
|
|
||||||
|
|
@ -1,141 +0,0 @@
|
||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: freek
|
|
||||||
* Date: 2018-11-23
|
|
||||||
* Time: 22:01
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets\Server;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Psr\Http\Message\RequestInterface;
|
|
||||||
use Ratchet\ConnectionInterface;
|
|
||||||
use Ratchet\Http\HttpServerInterface;
|
|
||||||
use Ratchet\RFC6455\Messaging\Message;
|
|
||||||
use Ratchet\RFC6455\Messaging\MessageInterface;
|
|
||||||
use Ratchet\WebSocket\MessageComponentInterface;
|
|
||||||
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
|
||||||
|
|
||||||
class Logger implements MessageComponentInterface
|
|
||||||
{
|
|
||||||
/** @var \Ratchet\Http\HttpServerInterface */
|
|
||||||
protected $app;
|
|
||||||
|
|
||||||
/** @var \Symfony\Component\Console\Output\OutputInterface */
|
|
||||||
protected $consoleOutput;
|
|
||||||
|
|
||||||
/** @var bool */
|
|
||||||
protected $enabled = false;
|
|
||||||
|
|
||||||
/** @var bool */
|
|
||||||
protected $verbose = false;
|
|
||||||
|
|
||||||
public static function decorate(MessageComponentInterface $app): Logger
|
|
||||||
{
|
|
||||||
$logger = app(Logger::class);
|
|
||||||
|
|
||||||
return $logger->setApp($app);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function isEnabled(): bool
|
|
||||||
{
|
|
||||||
return app(Logger::class)->enabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function __construct(OutputInterface $consoleOutput)
|
|
||||||
{
|
|
||||||
$this->consoleOutput = $consoleOutput;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function enable($enabled = true)
|
|
||||||
{
|
|
||||||
$this->enabled = $enabled;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function verbose($verbose = false)
|
|
||||||
{
|
|
||||||
$this->verbose = $verbose;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setApp(MessageComponentInterface $app)
|
|
||||||
{
|
|
||||||
$this->app = $app;
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onOpen(ConnectionInterface $connection)
|
|
||||||
{
|
|
||||||
$request = $connection->httpRequest;
|
|
||||||
|
|
||||||
$queryParameters = [];
|
|
||||||
parse_str($request->getUri()->getQuery(), $queryParameters);
|
|
||||||
|
|
||||||
$this->warn("New connection opened for app key {$queryParameters['appKey']}.");
|
|
||||||
|
|
||||||
$this->app->onOpen($connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onMessage(ConnectionInterface $connection, MessageInterface $message)
|
|
||||||
{
|
|
||||||
$this->info("{$connection->appId}: connection id {$connection->socketId} received message: {$message->getPayload()}.");
|
|
||||||
|
|
||||||
$this->app->onMessage($connection, $message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onClose(ConnectionInterface $connection)
|
|
||||||
{
|
|
||||||
$this->warn("{$connection->appId}: connection id {$connection->socketId} closed.");
|
|
||||||
|
|
||||||
$this->app->onClose($connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onError(ConnectionInterface $connection, Exception $exception)
|
|
||||||
{
|
|
||||||
$exceptionClass = get_class($exception);
|
|
||||||
|
|
||||||
$appId = $connection->appId ?? 'Unknown app id';
|
|
||||||
|
|
||||||
$message = "{$appId}: exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`";
|
|
||||||
|
|
||||||
if ($this->verbose) {
|
|
||||||
$message .= $exception->getTraceAsString();
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->error($message);
|
|
||||||
|
|
||||||
$this->app->onError($connection, $exception);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function info(string $message)
|
|
||||||
{
|
|
||||||
$this->line($message, 'info');
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function warn(string $message)
|
|
||||||
{
|
|
||||||
$this->line($message, 'warning');
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function error(string $message)
|
|
||||||
{
|
|
||||||
$this->line($message, 'error');
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function line(string $message, string $style)
|
|
||||||
{
|
|
||||||
$styled = $style ? "<$style>$message</$style>" : $message;
|
|
||||||
|
|
||||||
$this->consoleOutput->writeln($styled);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,61 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace BeyondCode\LaravelWebSockets\Server\Logger;
|
||||||
|
|
||||||
|
use Ratchet\ConnectionInterface;
|
||||||
|
|
||||||
|
class ConnectionLogger extends Logger implements ConnectionInterface
|
||||||
|
{
|
||||||
|
/** @var \Ratchet\ConnectionInterface */
|
||||||
|
protected $connection;
|
||||||
|
|
||||||
|
public static function decorate(ConnectionInterface $app): ConnectionLogger
|
||||||
|
{
|
||||||
|
$logger = app(ConnectionLogger::class);
|
||||||
|
|
||||||
|
return $logger->setConnection($app);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setConnection(ConnectionInterface $connection)
|
||||||
|
{
|
||||||
|
$this->connection = $connection;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getConnection() {
|
||||||
|
return $this->connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function send($data)
|
||||||
|
{
|
||||||
|
$this->info("{$this->connection->appId}: connection id {$this->connection->socketId} sending message {$data}");
|
||||||
|
|
||||||
|
$this->connection->send($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function close()
|
||||||
|
{
|
||||||
|
$this->warn("{$this->connection->appId}: connection id {$this->connection->socketId} closing.");
|
||||||
|
|
||||||
|
$this->connection->close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __set($name, $value)
|
||||||
|
{
|
||||||
|
return $this->connection->$name = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __get($name)
|
||||||
|
{
|
||||||
|
return $this->connection->$name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __isset($name) {
|
||||||
|
return isset($this->connection->$name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __unset($name) {
|
||||||
|
unset($this->connection->$name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace BeyondCode\LaravelWebSockets\Server\Logger;
|
||||||
|
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
|
||||||
|
class Logger
|
||||||
|
{
|
||||||
|
/** @var \Symfony\Component\Console\Output\OutputInterface */
|
||||||
|
protected $consoleOutput;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
protected $enabled = false;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
protected $verbose = false;
|
||||||
|
|
||||||
|
public static function isEnabled(): bool
|
||||||
|
{
|
||||||
|
return app(MessageLogger::class)->enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __construct(OutputInterface $consoleOutput)
|
||||||
|
{
|
||||||
|
$this->consoleOutput = $consoleOutput;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function enable($enabled = true)
|
||||||
|
{
|
||||||
|
$this->enabled = $enabled;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function verbose($verbose = false)
|
||||||
|
{
|
||||||
|
$this->verbose = $verbose;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function info(string $message)
|
||||||
|
{
|
||||||
|
$this->line($message, 'info');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function warn(string $message)
|
||||||
|
{
|
||||||
|
$this->line($message, 'warning');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function error(string $message)
|
||||||
|
{
|
||||||
|
$this->line($message, 'error');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function line(string $message, string $style)
|
||||||
|
{
|
||||||
|
$styled = $style ? "<$style>$message</$style>" : $message;
|
||||||
|
|
||||||
|
$this->consoleOutput->writeln($styled);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,78 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: freek
|
||||||
|
* Date: 2018-11-23
|
||||||
|
* Time: 22:01
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace BeyondCode\LaravelWebSockets\Server\Logger;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Ratchet\ConnectionInterface;
|
||||||
|
use Ratchet\RFC6455\Messaging\MessageInterface;
|
||||||
|
use Ratchet\WebSocket\MessageComponentInterface;
|
||||||
|
|
||||||
|
class MessageLogger extends Logger implements MessageComponentInterface
|
||||||
|
{
|
||||||
|
/** @var \Ratchet\Http\HttpServerInterface */
|
||||||
|
protected $app;
|
||||||
|
|
||||||
|
public static function decorate(MessageComponentInterface $app): MessageLogger
|
||||||
|
{
|
||||||
|
$logger = app(MessageLogger::class);
|
||||||
|
|
||||||
|
return $logger->setApp($app);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setApp(MessageComponentInterface $app)
|
||||||
|
{
|
||||||
|
$this->app = $app;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onOpen(ConnectionInterface $connection)
|
||||||
|
{
|
||||||
|
$request = $connection->httpRequest;
|
||||||
|
|
||||||
|
$queryParameters = [];
|
||||||
|
parse_str($request->getUri()->getQuery(), $queryParameters);
|
||||||
|
|
||||||
|
$this->warn("New connection opened for app key {$queryParameters['appKey']}.");
|
||||||
|
|
||||||
|
$this->app->onOpen(ConnectionLogger::decorate($connection));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onMessage(ConnectionInterface $connection, MessageInterface $message)
|
||||||
|
{
|
||||||
|
$this->info("{$connection->appId}: connection id {$connection->socketId} received message: {$message->getPayload()}.");
|
||||||
|
|
||||||
|
$this->app->onMessage(ConnectionLogger::decorate($connection), $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onClose(ConnectionInterface $connection)
|
||||||
|
{
|
||||||
|
$this->warn("{$connection->appId}: connection id {$connection->socketId} closed.");
|
||||||
|
|
||||||
|
$this->app->onClose(ConnectionLogger::decorate($connection));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onError(ConnectionInterface $connection, Exception $exception)
|
||||||
|
{
|
||||||
|
$exceptionClass = get_class($exception);
|
||||||
|
|
||||||
|
$appId = $connection->appId ?? 'Unknown app id';
|
||||||
|
|
||||||
|
$message = "{$appId}: exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`";
|
||||||
|
|
||||||
|
if ($this->verbose) {
|
||||||
|
$message .= $exception->getTraceAsString();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->error($message);
|
||||||
|
|
||||||
|
$this->app->onError(ConnectionLogger::decorate($connection), $exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebsockets\Server;
|
namespace BeyondCode\LaravelWebSockets\Server;
|
||||||
|
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
use Ratchet\Http\CloseResponseTrait;
|
use Ratchet\Http\CloseResponseTrait;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue