diff --git a/docs/advanced-usage/custom-websocket-handlers.md b/docs/advanced-usage/custom-websocket-handlers.md index 71ebe60c81..e41ed628a7 100644 --- a/docs/advanced-usage/custom-websocket-handlers.md +++ b/docs/advanced-usage/custom-websocket-handlers.md @@ -53,7 +53,7 @@ This class takes care of registering the routes with the actual webSocket server This could, for example, be done inside your `routes/web.php` file. ```php -WebSocketsRouter::get('/my-websocket', \App\MyCustomWebSocketHandler::class); +WebSocketsRouter::addCustomRoute('GET', '/my-websocket', \App\MyCustomWebSocketHandler::class); ``` Once you've added the custom WebSocket route, be sure to restart our WebSocket server for the changes to take place. diff --git a/src/Console/Commands/StartServer.php b/src/Console/Commands/StartServer.php index a0daec4768..abe1d075a6 100644 --- a/src/Console/Commands/StartServer.php +++ b/src/Console/Commands/StartServer.php @@ -158,7 +158,7 @@ public function configureRestartTimer() */ protected function configureRoutes() { - WebSocketRouter::routes(); + WebSocketRouter::registerRoutes(); } /** diff --git a/src/Server/Router.php b/src/Server/Router.php index bda9878bdd..3092f7cf2e 100644 --- a/src/Server/Router.php +++ b/src/Server/Router.php @@ -3,6 +3,7 @@ namespace BeyondCode\LaravelWebSockets\Server; use BeyondCode\LaravelWebSockets\Server\Loggers\WebSocketsLogger; +use Illuminate\Support\Collection; use Ratchet\WebSocket\MessageComponentInterface; use Ratchet\WebSocket\WsServer; use Symfony\Component\Routing\Route; @@ -17,6 +18,13 @@ class Router */ protected $routes; + /** + * Define the custom routes. + * + * @var array + */ + protected $customRoutes; + /** * Initialize the class. * @@ -25,6 +33,14 @@ class Router public function __construct() { $this->routes = new RouteCollection; + + $this->customRoutes = [ + 'get' => new Collection, + 'post' => new Collection, + 'put' => new Collection, + 'patch' => new Collection, + 'delete' => new Collection, + ]; } /** @@ -37,12 +53,22 @@ public function getRoutes(): RouteCollection return $this->routes; } + /** + * Get the list of routes that still need to be registered. + * + * @return array[Collection] + */ + public function getCustomRoutes(): array + { + return $this->customRoutes; + } + /** * Register the default routes. * * @return void */ - public function routes() + public function registerRoutes() { $this->get('/app/{appKey}', config('websockets.handlers.websocket')); $this->post('/apps/{appId}/events', config('websockets.handlers.trigger_event')); @@ -50,6 +76,8 @@ public function routes() $this->get('/apps/{appId}/channels/{channelName}', config('websockets.handlers.fetch_channel')); $this->get('/apps/{appId}/channels/{channelName}/users', config('websockets.handlers.fetch_users')); $this->get('/health', config('websockets.handlers.health')); + + $this->registerCustomRoutes(); } /** @@ -125,6 +153,34 @@ public function addRoute(string $method, string $uri, $action) $this->routes->add($uri, $this->getRoute($method, $uri, $action)); } + /** + * Add a new custom route. Registered routes + * will be resolved at server spin-up. + * + * @param string $method + * @param string $uri + * @param string $action + * @return void + */ + public function addCustomRoute(string $method, $uri, $action) + { + $this->customRoutes[strtolower($method)]->put($uri, $action); + } + + /** + * Register the custom routes into the main RouteCollection. + * + * @return void + */ + public function registerCustomRoutes() + { + foreach ($this->customRoutes as $method => $actions) { + $actions->each(function ($action, $uri) use ($method) { + $this->{$method}($uri, $action); + }); + } + } + /** * Get the route of a specified method, uri and action. * diff --git a/tests/Handlers/TestHandler.php b/tests/Handlers/TestHandler.php new file mode 100644 index 0000000000..6a43052192 --- /dev/null +++ b/tests/Handlers/TestHandler.php @@ -0,0 +1,31 @@ +close(); + } + + public function onClose(ConnectionInterface $connection) + { + // + } + + public function onError(ConnectionInterface $connection, Exception $e) + { + dump($e->getMessage()); + } + + public function onMessage(ConnectionInterface $connection, MessageInterface $msg) + { + dump($msg); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index bcf7e287d6..da66201873 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,6 +5,7 @@ use BeyondCode\LaravelWebSockets\Contracts\ChannelManager; use BeyondCode\LaravelWebSockets\Contracts\StatisticsCollector; use BeyondCode\LaravelWebSockets\Contracts\StatisticsStore; +use BeyondCode\LaravelWebSockets\Facades\WebSocketRouter; use BeyondCode\LaravelWebSockets\Helpers; use GuzzleHttp\Psr7\Request; use Illuminate\Support\Facades\Redis; @@ -78,6 +79,8 @@ public function setUp(): void $this->loadMigrationsFrom(__DIR__.'/database/migrations'); $this->withFactories(__DIR__.'/database/factories'); + $this->registerCustomPath(); + $this->registerPromiseResolver(); $this->registerManagers(); @@ -218,6 +221,20 @@ public function getEnvironmentSetUp($app) ]); } + /** + * Register custom paths. + * + * @return void + */ + protected function registerCustomPath() + { + WebSocketRouter::addCustomRoute('GET', '/test', Handlers\TestHandler::class); + WebSocketRouter::addCustomRoute('POST', '/test', Handlers\TestHandler::class); + WebSocketRouter::addCustomRoute('PUT', '/test', Handlers\TestHandler::class); + WebSocketRouter::addCustomRoute('PATCH', '/test', Handlers\TestHandler::class); + WebSocketRouter::addCustomRoute('DELETE', '/test', Handlers\TestHandler::class); + } + /** * Register the test promise resolver. *