diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index 7e221ae..43e7226 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -3,7 +3,8 @@ namespace BeyondCode\LaravelWebSockets\Console; 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 BeyondCode\LaravelWebSockets\Server\WebSocketServer; @@ -18,15 +19,28 @@ class StartWebSocketServer extends Command public function handle() { $this - ->configureLogger() + ->configureMessageLogger() + ->configureConnectionLogger() ->registerEchoRoutes() ->startWebSocketServer(); } - protected function configureLogger() + protected function configureMessageLogger() { - app()->singleton(Logger::class, function() { - return (new Logger($this->output)) + app()->singleton(MessageLogger::class, function() { + 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')) //TODO: use real option ->verbose($this->hasOption('vvv')); @@ -49,7 +63,7 @@ class StartWebSocketServer extends Command $routes = WebSocketRouter::getRoutes(); /** 🎩 Start the magic 🎩 */ - return (new WebSocketServer($routes)) + (new WebSocketServer($routes)) ->setHost($this->option('host')) ->setPort($this->option('port')) ->setConsoleOutput($this->output) diff --git a/src/LaravelEcho/Http/Controllers/EchoController.php b/src/LaravelEcho/Http/Controllers/EchoController.php index 8cf014d..dd0200a 100644 --- a/src/LaravelEcho/Http/Controllers/EchoController.php +++ b/src/LaravelEcho/Http/Controllers/EchoController.php @@ -66,7 +66,7 @@ abstract class EchoController implements HttpServerInterface 'error' => $exception->getMessage() ])); - $connection->send(Psr\str($response)); + $connection->send(gPsr\str($response)); $connection->close(); } } diff --git a/src/LaravelEcho/Pusher/Exceptions/PusherException.php b/src/LaravelEcho/Pusher/Exceptions/PusherException.php index 12c2933..e14f060 100644 --- a/src/LaravelEcho/Pusher/Exceptions/PusherException.php +++ b/src/LaravelEcho/Pusher/Exceptions/PusherException.php @@ -1,6 +1,6 @@ getPayload()); - // Log this for now - dump($payload); - return starts_with($payload->event, 'pusher:') ? new PusherMessage($payload, $connection, $channelManager) : new Message($payload, $connection, $channelManager); diff --git a/src/Router.php b/src/Router.php index da36156..3f07418 100644 --- a/src/Router.php +++ b/src/Router.php @@ -2,7 +2,7 @@ namespace BeyondCode\LaravelWebSockets; -use BeyondCode\LaravelWebSockets\Server\Logger; +use BeyondCode\LaravelWebSockets\Server\Logger\MessageLogger; use Ratchet\WebSocket\WsServer; use Symfony\Component\Routing\Route; use Ratchet\Http\HttpServerInterface; @@ -83,8 +83,8 @@ class Router if (is_subclass_of($action, WebSocketController::class)) { $app = app($action); - if (Logger::isEnabled()) { - $app = Logger::decorate($app); + if (MessageLogger::isEnabled()) { + $app = MessageLogger::decorate($app); } return new WsServer($app); diff --git a/src/Server/Logger.php b/src/Server/Logger.php deleted file mode 100644 index 600f447..0000000 --- a/src/Server/Logger.php +++ /dev/null @@ -1,141 +0,0 @@ -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" : $message; - - $this->consoleOutput->writeln($styled); - } - - - - -} \ No newline at end of file diff --git a/src/Server/Logger/ConnectionLogger.php b/src/Server/Logger/ConnectionLogger.php new file mode 100644 index 0000000..a0c3b53 --- /dev/null +++ b/src/Server/Logger/ConnectionLogger.php @@ -0,0 +1,61 @@ +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); + } +} \ No newline at end of file diff --git a/src/Server/Logger/Logger.php b/src/Server/Logger/Logger.php new file mode 100644 index 0000000..b1dcde8 --- /dev/null +++ b/src/Server/Logger/Logger.php @@ -0,0 +1,63 @@ +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" : $message; + + $this->consoleOutput->writeln($styled); + } +} \ No newline at end of file diff --git a/src/Server/Logger/MessageLogger.php b/src/Server/Logger/MessageLogger.php new file mode 100644 index 0000000..a463d29 --- /dev/null +++ b/src/Server/Logger/MessageLogger.php @@ -0,0 +1,78 @@ +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); + } + +} \ No newline at end of file diff --git a/src/Server/OriginCheck.php b/src/Server/OriginCheck.php index 7b8bfdb..689ef07 100644 --- a/src/Server/OriginCheck.php +++ b/src/Server/OriginCheck.php @@ -1,6 +1,6 @@