Fixed Routes registration
This commit is contained in:
parent
ba3a2ad164
commit
c43a9d4d46
|
|
@ -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.
|
This could, for example, be done inside your `routes/web.php` file.
|
||||||
|
|
||||||
```php
|
```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.
|
Once you've added the custom WebSocket route, be sure to restart our WebSocket server for the changes to take place.
|
||||||
|
|
|
||||||
|
|
@ -158,7 +158,7 @@ class StartServer extends Command
|
||||||
*/
|
*/
|
||||||
protected function configureRoutes()
|
protected function configureRoutes()
|
||||||
{
|
{
|
||||||
WebSocketRouter::routes();
|
WebSocketRouter::registerRoutes();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
namespace BeyondCode\LaravelWebSockets\Server;
|
namespace BeyondCode\LaravelWebSockets\Server;
|
||||||
|
|
||||||
use BeyondCode\LaravelWebSockets\Server\Loggers\WebSocketsLogger;
|
use BeyondCode\LaravelWebSockets\Server\Loggers\WebSocketsLogger;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
use Ratchet\WebSocket\MessageComponentInterface;
|
use Ratchet\WebSocket\MessageComponentInterface;
|
||||||
use Ratchet\WebSocket\WsServer;
|
use Ratchet\WebSocket\WsServer;
|
||||||
use Symfony\Component\Routing\Route;
|
use Symfony\Component\Routing\Route;
|
||||||
|
|
@ -17,6 +18,13 @@ class Router
|
||||||
*/
|
*/
|
||||||
protected $routes;
|
protected $routes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define the custom routes.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $customRoutes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the class.
|
* Initialize the class.
|
||||||
*
|
*
|
||||||
|
|
@ -25,6 +33,14 @@ class Router
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->routes = new RouteCollection;
|
$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 @@ class Router
|
||||||
return $this->routes;
|
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.
|
* Register the default routes.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function routes()
|
public function registerRoutes()
|
||||||
{
|
{
|
||||||
$this->get('/app/{appKey}', config('websockets.handlers.websocket'));
|
$this->get('/app/{appKey}', config('websockets.handlers.websocket'));
|
||||||
$this->post('/apps/{appId}/events', config('websockets.handlers.trigger_event'));
|
$this->post('/apps/{appId}/events', config('websockets.handlers.trigger_event'));
|
||||||
|
|
@ -50,6 +76,8 @@ class Router
|
||||||
$this->get('/apps/{appId}/channels/{channelName}', config('websockets.handlers.fetch_channel'));
|
$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('/apps/{appId}/channels/{channelName}/users', config('websockets.handlers.fetch_users'));
|
||||||
$this->get('/health', config('websockets.handlers.health'));
|
$this->get('/health', config('websockets.handlers.health'));
|
||||||
|
|
||||||
|
$this->registerCustomRoutes();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -125,6 +153,34 @@ class Router
|
||||||
$this->routes->add($uri, $this->getRoute($method, $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.
|
* Get the route of a specified method, uri and action.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,6 +5,7 @@ namespace BeyondCode\LaravelWebSockets\Test;
|
||||||
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager;
|
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager;
|
||||||
use BeyondCode\LaravelWebSockets\Contracts\StatisticsCollector;
|
use BeyondCode\LaravelWebSockets\Contracts\StatisticsCollector;
|
||||||
use BeyondCode\LaravelWebSockets\Contracts\StatisticsStore;
|
use BeyondCode\LaravelWebSockets\Contracts\StatisticsStore;
|
||||||
|
use BeyondCode\LaravelWebSockets\Facades\WebSocketRouter;
|
||||||
use BeyondCode\LaravelWebSockets\Helpers;
|
use BeyondCode\LaravelWebSockets\Helpers;
|
||||||
use GuzzleHttp\Psr7\Request;
|
use GuzzleHttp\Psr7\Request;
|
||||||
use Illuminate\Support\Facades\Redis;
|
use Illuminate\Support\Facades\Redis;
|
||||||
|
|
@ -78,6 +79,8 @@ abstract class TestCase extends Orchestra
|
||||||
$this->loadMigrationsFrom(__DIR__.'/database/migrations');
|
$this->loadMigrationsFrom(__DIR__.'/database/migrations');
|
||||||
$this->withFactories(__DIR__.'/database/factories');
|
$this->withFactories(__DIR__.'/database/factories');
|
||||||
|
|
||||||
|
$this->registerCustomPath();
|
||||||
|
|
||||||
$this->registerPromiseResolver();
|
$this->registerPromiseResolver();
|
||||||
|
|
||||||
$this->registerManagers();
|
$this->registerManagers();
|
||||||
|
|
@ -218,6 +221,20 @@ abstract class TestCase extends Orchestra
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* Register the test promise resolver.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue