Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 556f433

Browse files
lukepolompociot
authored andcommitted
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
1 parent 5e84ef1 commit 556f433

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/Console/StartWebSocketServer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function handle()
4444
->configureMessageLogger()
4545
->configureConnectionLogger()
4646
->registerEchoRoutes()
47+
->registerCustomRoutes()
4748
->startWebSocketServer();
4849
}
4950

@@ -110,6 +111,13 @@ protected function registerEchoRoutes()
110111
return $this;
111112
}
112113

114+
protected function registerCustomRoutes()
115+
{
116+
WebSocketsRouter::customRoutes();
117+
118+
return $this;
119+
}
120+
113121
protected function startWebSocketServer()
114122
{
115123
$this->info("Starting the WebSocket server on port {$this->option('port')}...");

src/Server/Router.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BeyondCode\LaravelWebSockets\Server;
44

55
use Ratchet\WebSocket\WsServer;
6+
use Illuminate\Support\Collection;
67
use Symfony\Component\Routing\Route;
78
use Symfony\Component\Routing\RouteCollection;
89
use Ratchet\WebSocket\MessageComponentInterface;
@@ -18,10 +19,12 @@ class Router
1819
{
1920
/** @var \Symfony\Component\Routing\RouteCollection */
2021
protected $routes;
22+
protected $customRoutes;
2123

2224
public function __construct()
2325
{
2426
$this->routes = new RouteCollection;
27+
$this->customRoutes = new Collection();
2528
}
2629

2730
public function getRoutes(): RouteCollection
@@ -39,6 +42,13 @@ public function echo()
3942
$this->get('/apps/{appId}/channels/{channelName}/users', FetchUsersController::class);
4043
}
4144

45+
public function customRoutes()
46+
{
47+
$this->customRoutes->each(function ($action, $uri) {
48+
$this->get($uri, $action);
49+
});
50+
}
51+
4252
public function get(string $uri, $action)
4353
{
4454
$this->addRoute('GET', $uri, $action);
@@ -70,7 +80,7 @@ public function webSocket(string $uri, $action)
7080
throw InvalidWebSocketController::withController($action);
7181
}
7282

73-
$this->get($uri, $action);
83+
$this->customRoutes->put($uri, $action);
7484
}
7585

7686
public function addRoute(string $method, string $uri, $action)

0 commit comments

Comments
 (0)