diff --git a/config/websockets.php b/config/websockets.php index 85bdf28..6ca0917 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -4,6 +4,11 @@ use BeyondCode\LaravelWebSockets\ClientProviders\ConfigClientProvider; return [ + /* + * Path for the Websockets debug console + */ + 'path' => '/websockets', + /* * TODO: add the laravel style comment here */ diff --git a/resources/views/console.blade.php b/resources/views/console.blade.php new file mode 100644 index 0000000..a9bb95f --- /dev/null +++ b/resources/views/console.blade.php @@ -0,0 +1,83 @@ + + + + WebSockets Console + + + + + + +
+
+
+
+ WebSockets Console +
+
+ + +
+
+ + +
+ +
+
+ +
+
+
+

Events

+ + + + + + + + + + + +
TypeSocketDetailsTime
+
+
+
+ + + \ No newline at end of file diff --git a/src/Http/Controllers/AuthenticateConsole.php b/src/Http/Controllers/AuthenticateConsole.php new file mode 100644 index 0000000..12e3cd0 --- /dev/null +++ b/src/Http/Controllers/AuthenticateConsole.php @@ -0,0 +1,14 @@ +validAuthenticationResponse($request, []); + } +} \ No newline at end of file diff --git a/src/Http/Controllers/ShowConsole.php b/src/Http/Controllers/ShowConsole.php new file mode 100644 index 0000000..ebeb262 --- /dev/null +++ b/src/Http/Controllers/ShowConsole.php @@ -0,0 +1,13 @@ +verifySignature($connection, $payload); + + $this->verifyAdministrator($connection); + + parent::subscribe($connection, $payload); + } + + protected function verifyAdministrator(ConnectionInterface $connection) + { + throw_unless($connection->isAdmin, new InvalidConnectionException()); + } +} \ No newline at end of file diff --git a/src/LaravelEcho/Pusher/Exceptions/InvalidConnectionException.php b/src/LaravelEcho/Pusher/Exceptions/InvalidConnectionException.php new file mode 100644 index 0000000..143270c --- /dev/null +++ b/src/LaravelEcho/Pusher/Exceptions/InvalidConnectionException.php @@ -0,0 +1,12 @@ +message = 'Invalid Connection'; + $this->code = 4009; + } +} \ No newline at end of file diff --git a/src/LaravelEcho/Pusher/Exceptions/UnknownAppKey.php b/src/LaravelEcho/Pusher/Exceptions/UnknownAppKeyException.php similarity index 81% rename from src/LaravelEcho/Pusher/Exceptions/UnknownAppKey.php rename to src/LaravelEcho/Pusher/Exceptions/UnknownAppKeyException.php index 76e856f..c154d27 100644 --- a/src/LaravelEcho/Pusher/Exceptions/UnknownAppKey.php +++ b/src/LaravelEcho/Pusher/Exceptions/UnknownAppKeyException.php @@ -2,7 +2,7 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions; -class UnknownAppKey extends PusherException +class UnknownAppKeyException extends PusherException { public function __construct(string $appKey) { diff --git a/src/LaravelEcho/WebSocket/ConsoleServer.php b/src/LaravelEcho/WebSocket/ConsoleServer.php new file mode 100644 index 0000000..34beb5a --- /dev/null +++ b/src/LaravelEcho/WebSocket/ConsoleServer.php @@ -0,0 +1,36 @@ +generateSocketId($connection); + + $this->verifyConnection($connection); + + $this->establishConnection($connection); + + // TODO check connection signature + $connection->isAdmin = true; + } + + public function log(string $appId, string $type, string $details) + { + $channelId = "private-logger-{$type}"; + + $channel = $this->channelManager->find($appId, $channelId); + + optional($channel)->broadcast([ + 'event' => $type, + 'channel' => $channelId, + 'data' => [ + 'type' => $type, + 'details' => $details + ] + ]); + } +} \ No newline at end of file diff --git a/src/LaravelEcho/WebSocket/PusherServer.php b/src/LaravelEcho/WebSocket/PusherServer.php index f48027f..9cab45d 100644 --- a/src/LaravelEcho/WebSocket/PusherServer.php +++ b/src/LaravelEcho/WebSocket/PusherServer.php @@ -10,15 +10,22 @@ use Ratchet\ConnectionInterface; use Ratchet\RFC6455\Messaging\MessageInterface; use BeyondCode\LaravelWebSockets\WebSocketController; use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager; +use BeyondCode\LaravelWebsockets\LaravelEcho\Pusher\Exceptions\PusherException; +use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\UnknownAppKeyException; class PusherServer extends WebSocketController { /** @var \BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager */ protected $channelManager; - public function __construct(ChannelManager $channelManager) + /** @var ConsoleServer|null */ + protected $consoleServer; + + public function __construct(ChannelManager $channelManager, ConsoleServer $consoleServer = null) { $this->channelManager = $channelManager; + + $this->consoleServer = $consoleServer; } function onOpen(ConnectionInterface $connection) @@ -28,6 +35,8 @@ class PusherServer extends WebSocketController $this->verifyConnection($connection); $this->establishConnection($connection); + + $this->consoleServer->log($connection->appId, 'new_connection', ''); } public function onMessage(ConnectionInterface $connection, MessageInterface $message) diff --git a/src/LaravelWebSocketsServiceProvider.php b/src/LaravelWebSocketsServiceProvider.php index c96fe8b..b980814 100644 --- a/src/LaravelWebSocketsServiceProvider.php +++ b/src/LaravelWebSocketsServiceProvider.php @@ -2,8 +2,10 @@ namespace BeyondCode\LaravelWebSockets; +use Illuminate\Support\Facades\Route; use BeyondCode\LaravelWebSockets\ClientProviders\ClientProvider; use Illuminate\Support\ServiceProvider; +use BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket\ConsoleServer; use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager; class LaravelWebSocketsServiceProvider extends ServiceProvider @@ -14,11 +16,30 @@ class LaravelWebSocketsServiceProvider extends ServiceProvider __DIR__.'/../config/websockets.php' => base_path('config/websockets.php'), ], 'config'); + $this->registerRoutes(); + + $this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets'); + $this->commands([ Console\StartWebSocketServer::class, ]); } + protected function registerRoutes() + { + Route::group($this->routeConfiguration(), function () { + $this->loadRoutesFrom(__DIR__.'/Http/routes.php'); + }); + } + + protected function routeConfiguration() + { + return [ + 'namespace' => 'BeyondCode\LaravelWebSockets\Http\Controllers', + 'prefix' => config('websockets.path'), + ]; + } + public function register() { $this->mergeConfigFrom(__DIR__.'/../config/websockets.php', 'websockets'); @@ -34,5 +55,9 @@ class LaravelWebSocketsServiceProvider extends ServiceProvider $this->app->singleton(ClientProvider::class, function() { return app(config('websockets.client_provider')); }); + + $this->app->singleton(ConsoleServer::class, function() { + return new ConsoleServer(new ChannelManager()); + }); } } diff --git a/src/Router.php b/src/Router.php index 3f07418..2f3147e 100644 --- a/src/Router.php +++ b/src/Router.php @@ -67,6 +67,8 @@ class Router { $this->get('/app/{appKey}', LaravelEcho\WebSocket\PusherServer::class); + $this->get('/console/app/{appKey}', LaravelEcho\WebSocket\ConsoleServer::class); + $this->get('/apps/{appId}/channels', LaravelEcho\Http\Controllers\FetchChannels::class); $this->get('/apps/{appId}/channels/{channelName}', LaravelEcho\Http\Controllers\FetchChannel::class); $this->get('/apps/{appId}/channels/{channelName}/users', LaravelEcho\Http\Controllers\FetchUsers::class);