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

Commit

Permalink
Fixed Routes registration
Browse files Browse the repository at this point in the history
  • Loading branch information
rennokki committed Apr 6, 2021
1 parent ba3a2ad commit c43a9d4
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/advanced-usage/custom-websocket-handlers.md
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion src/Console/Commands/StartServer.php
Expand Up @@ -158,7 +158,7 @@ public function configureRestartTimer()
*/
protected function configureRoutes()
{
WebSocketRouter::routes();
WebSocketRouter::registerRoutes();
}

/**
Expand Down
58 changes: 57 additions & 1 deletion src/Server/Router.php
Expand Up @@ -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;
Expand All @@ -17,6 +18,13 @@ class Router
*/
protected $routes;

/**
* Define the custom routes.
*
* @var array
*/
protected $customRoutes;

/**
* Initialize the class.
*
Expand All @@ -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,
];
}

/**
Expand All @@ -37,19 +53,31 @@ 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'));
$this->get('/apps/{appId}/channels', config('websockets.handlers.fetch_channels'));
$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();
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
31 changes: 31 additions & 0 deletions tests/Handlers/TestHandler.php
@@ -0,0 +1,31 @@
<?php

namespace BeyondCode\LaravelWebSockets\Test\Handlers;

use Exception;
use Ratchet\ConnectionInterface;
use Ratchet\RFC6455\Messaging\MessageInterface;
use Ratchet\WebSocket\MessageComponentInterface;

class TestHandler implements MessageComponentInterface
{
public function onOpen(ConnectionInterface $connection)
{
$connection->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);
}
}
17 changes: 17 additions & 0 deletions tests/TestCase.php
Expand Up @@ -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;
Expand Down Expand Up @@ -78,6 +79,8 @@ public function setUp(): void
$this->loadMigrationsFrom(__DIR__.'/database/migrations');
$this->withFactories(__DIR__.'/database/factories');

$this->registerCustomPath();

$this->registerPromiseResolver();

$this->registerManagers();
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit c43a9d4

Please sign in to comment.