From 556f4338e8fd2505a1b9ed761d7b08a9b1316c5f Mon Sep 17 00:00:00 2001 From: Luke Policinski Date: Sat, 11 May 2019 02:46:25 -0400 Subject: [PATCH] Fix/Feature : Fixing ability to add custom handlers to a route by adding a custom routes method to the router (#150) 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 c91fecc..d33dc1e 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -44,6 +44,7 @@ class StartWebSocketServer extends Command ->configureMessageLogger() ->configureConnectionLogger() ->registerEchoRoutes() + ->registerCustomRoutes() ->startWebSocketServer(); } @@ -110,6 +111,13 @@ class StartWebSocketServer extends Command 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 25dbb77..3ce6685 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 @@ class Router $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 @@ class Router throw InvalidWebSocketController::withController($action); } - $this->get($uri, $action); + $this->customRoutes->put($uri, $action); } public function addRoute(string $method, string $uri, $action)