Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit c43a9d4

Browse files
committed
Fixed Routes registration
1 parent ba3a2ad commit c43a9d4

File tree

5 files changed

+107
-3
lines changed

5 files changed

+107
-3
lines changed

‎docs/advanced-usage/custom-websocket-handlers.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ This class takes care of registering the routes with the actual webSocket server
5353
This could, for example, be done inside your `routes/web.php` file.
5454

5555
```php
56-
WebSocketsRouter::get('/my-websocket', \App\MyCustomWebSocketHandler::class);
56+
WebSocketsRouter::addCustomRoute('GET', '/my-websocket', \App\MyCustomWebSocketHandler::class);
5757
```
5858

5959
Once you've added the custom WebSocket route, be sure to restart our WebSocket server for the changes to take place.

‎src/Console/Commands/StartServer.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function configureRestartTimer()
158158
*/
159159
protected function configureRoutes()
160160
{
161-
WebSocketRouter::routes();
161+
WebSocketRouter::registerRoutes();
162162
}
163163

164164
/**

‎src/Server/Router.php‎

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

55
use BeyondCode\LaravelWebSockets\Server\Loggers\WebSocketsLogger;
6+
use Illuminate\Support\Collection;
67
use Ratchet\WebSocket\MessageComponentInterface;
78
use Ratchet\WebSocket\WsServer;
89
use Symfony\Component\Routing\Route;
@@ -17,6 +18,13 @@ class Router
1718
*/
1819
protected $routes;
1920

21+
/**
22+
* Define the custom routes.
23+
*
24+
* @var array
25+
*/
26+
protected $customRoutes;
27+
2028
/**
2129
* Initialize the class.
2230
*
@@ -25,6 +33,14 @@ class Router
2533
public function __construct()
2634
{
2735
$this->routes = new RouteCollection;
36+
37+
$this->customRoutes = [
38+
'get' => new Collection,
39+
'post' => new Collection,
40+
'put' => new Collection,
41+
'patch' => new Collection,
42+
'delete' => new Collection,
43+
];
2844
}
2945

3046
/**
@@ -37,19 +53,31 @@ public function getRoutes(): RouteCollection
3753
return $this->routes;
3854
}
3955

56+
/**
57+
* Get the list of routes that still need to be registered.
58+
*
59+
* @return array[Collection]
60+
*/
61+
public function getCustomRoutes(): array
62+
{
63+
return $this->customRoutes;
64+
}
65+
4066
/**
4167
* Register the default routes.
4268
*
4369
* @return void
4470
*/
45-
public function routes()
71+
public function registerRoutes()
4672
{
4773
$this->get('/app/{appKey}', config('websockets.handlers.websocket'));
4874
$this->post('/apps/{appId}/events', config('websockets.handlers.trigger_event'));
4975
$this->get('/apps/{appId}/channels', config('websockets.handlers.fetch_channels'));
5076
$this->get('/apps/{appId}/channels/{channelName}', config('websockets.handlers.fetch_channel'));
5177
$this->get('/apps/{appId}/channels/{channelName}/users', config('websockets.handlers.fetch_users'));
5278
$this->get('/health', config('websockets.handlers.health'));
79+
80+
$this->registerCustomRoutes();
5381
}
5482

5583
/**
@@ -125,6 +153,34 @@ public function addRoute(string $method, string $uri, $action)
125153
$this->routes->add($uri, $this->getRoute($method, $uri, $action));
126154
}
127155

156+
/**
157+
* Add a new custom route. Registered routes
158+
* will be resolved at server spin-up.
159+
*
160+
* @param string $method
161+
* @param string $uri
162+
* @param string $action
163+
* @return void
164+
*/
165+
public function addCustomRoute(string $method, $uri, $action)
166+
{
167+
$this->customRoutes[strtolower($method)]->put($uri, $action);
168+
}
169+
170+
/**
171+
* Register the custom routes into the main RouteCollection.
172+
*
173+
* @return void
174+
*/
175+
public function registerCustomRoutes()
176+
{
177+
foreach ($this->customRoutes as $method => $actions) {
178+
$actions->each(function ($action, $uri) use ($method) {
179+
$this->{$method}($uri, $action);
180+
});
181+
}
182+
}
183+
128184
/**
129185
* Get the route of a specified method, uri and action.
130186
*

‎tests/Handlers/TestHandler.php‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Test\Handlers;
4+
5+
use Exception;
6+
use Ratchet\ConnectionInterface;
7+
use Ratchet\RFC6455\Messaging\MessageInterface;
8+
use Ratchet\WebSocket\MessageComponentInterface;
9+
10+
class TestHandler implements MessageComponentInterface
11+
{
12+
public function onOpen(ConnectionInterface $connection)
13+
{
14+
$connection->close();
15+
}
16+
17+
public function onClose(ConnectionInterface $connection)
18+
{
19+
//
20+
}
21+
22+
public function onError(ConnectionInterface $connection, Exception $e)
23+
{
24+
dump($e->getMessage());
25+
}
26+
27+
public function onMessage(ConnectionInterface $connection, MessageInterface $msg)
28+
{
29+
dump($msg);
30+
}
31+
}

‎tests/TestCase.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager;
66
use BeyondCode\LaravelWebSockets\Contracts\StatisticsCollector;
77
use BeyondCode\LaravelWebSockets\Contracts\StatisticsStore;
8+
use BeyondCode\LaravelWebSockets\Facades\WebSocketRouter;
89
use BeyondCode\LaravelWebSockets\Helpers;
910
use GuzzleHttp\Psr7\Request;
1011
use Illuminate\Support\Facades\Redis;
@@ -78,6 +79,8 @@ public function setUp(): void
7879
$this->loadMigrationsFrom(__DIR__.'/database/migrations');
7980
$this->withFactories(__DIR__.'/database/factories');
8081

82+
$this->registerCustomPath();
83+
8184
$this->registerPromiseResolver();
8285

8386
$this->registerManagers();
@@ -218,6 +221,20 @@ public function getEnvironmentSetUp($app)
218221
]);
219222
}
220223

224+
/**
225+
* Register custom paths.
226+
*
227+
* @return void
228+
*/
229+
protected function registerCustomPath()
230+
{
231+
WebSocketRouter::addCustomRoute('GET', '/test', Handlers\TestHandler::class);
232+
WebSocketRouter::addCustomRoute('POST', '/test', Handlers\TestHandler::class);
233+
WebSocketRouter::addCustomRoute('PUT', '/test', Handlers\TestHandler::class);
234+
WebSocketRouter::addCustomRoute('PATCH', '/test', Handlers\TestHandler::class);
235+
WebSocketRouter::addCustomRoute('DELETE', '/test', Handlers\TestHandler::class);
236+
}
237+
221238
/**
222239
* Register the test promise resolver.
223240
*

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /