From 31f9a9fc0fecfa316bc7a477348b162a61dd5824 Mon Sep 17 00:00:00 2001 From: Luke Policinski Date: Sat, 13 Apr 2019 08:41:08 -0400 Subject: [PATCH] Fix/Feature : Fixing ability to add custom handlers to a route by adding a custom routes method to the router Fixing Coding Standards Fixing Coding Standards --- src/Console/StartWebSocketServer.php | 8 ++++++++ src/Server/Router.php | 12 +++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index c91feccdf8..d33dc1e7f3 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -44,6 +44,7 @@ public function handle() ->configureMessageLogger() ->configureConnectionLogger() ->registerEchoRoutes() + ->registerCustomRoutes() ->startWebSocketServer(); } @@ -110,6 +111,13 @@ protected function registerEchoRoutes() return $this; } + protected function registerCustomRoutes() + { + WebSocketsRouter::customRoutes(); + + return $this; + } + protected function startWebSocketServer() { $this->info("Starting the WebSocket server on port {$this->option('port')}..."); diff --git a/src/Server/Router.php b/src/Server/Router.php index 25dbb77385..3ce668525e 100644 --- a/src/Server/Router.php +++ b/src/Server/Router.php @@ -3,6 +3,7 @@ namespace BeyondCode\LaravelWebSockets\Server; use Ratchet\WebSocket\WsServer; +use Illuminate\Support\Collection; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; use Ratchet\WebSocket\MessageComponentInterface; @@ -18,10 +19,12 @@ class Router { /** @var \Symfony\Component\Routing\RouteCollection */ protected $routes; + protected $customRoutes; public function __construct() { $this->routes = new RouteCollection; + $this->customRoutes = new Collection(); } public function getRoutes(): RouteCollection @@ -39,6 +42,13 @@ public function echo() $this->get('/apps/{appId}/channels/{channelName}/users', FetchUsersController::class); } + public function customRoutes() + { + $this->customRoutes->each(function ($action, $uri) { + $this->get($uri, $action); + }); + } + public function get(string $uri, $action) { $this->addRoute('GET', $uri, $action); @@ -70,7 +80,7 @@ public function webSocket(string $uri, $action) throw InvalidWebSocketController::withController($action); } - $this->get($uri, $action); + $this->customRoutes->put($uri, $action); } public function addRoute(string $method, string $uri, $action)