diff --git a/config/websockets.php b/config/websockets.php index 67efc05..ce79f2c 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -37,9 +37,9 @@ return [ ], /* - * The maximum request size that is allowed for an incoming websocket request. + * The maximum request size in bytes that is allowed for an incoming websocket request. */ - 'maxRequestSize' => 256000, + 'maxRequestSize' => 1024 * 250, /* * Define the optional SSL context for your websocket connections. diff --git a/src/HttpApi/Controllers/FetchChannel.php b/src/HttpApi/Controllers/FetchChannelController.php similarity index 91% rename from src/HttpApi/Controllers/FetchChannel.php rename to src/HttpApi/Controllers/FetchChannelController.php index 2cb44e3..af7a96e 100644 --- a/src/HttpApi/Controllers/FetchChannel.php +++ b/src/HttpApi/Controllers/FetchChannelController.php @@ -6,7 +6,7 @@ use BeyondCode\LaravelWebSockets\HttpApi\Controllers\Controller; use Illuminate\Http\Request; use Symfony\Component\HttpKernel\Exception\HttpException; -class FetchChannel extends Controller +class FetchChannelController extends Controller { public function __invoke(Request $request) { diff --git a/src/HttpApi/Controllers/FetchChannels.php b/src/HttpApi/Controllers/FetchChannelsController.php similarity index 95% rename from src/HttpApi/Controllers/FetchChannels.php rename to src/HttpApi/Controllers/FetchChannelsController.php index 6b0960d..26564d6 100644 --- a/src/HttpApi/Controllers/FetchChannels.php +++ b/src/HttpApi/Controllers/FetchChannelsController.php @@ -7,7 +7,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Collection; use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel; -class FetchChannels extends Controller +class FetchChannelsController extends Controller { public function __invoke(Request $request) { diff --git a/src/HttpApi/Controllers/FetchUsers.php b/src/HttpApi/Controllers/FetchUsersController.php similarity index 95% rename from src/HttpApi/Controllers/FetchUsers.php rename to src/HttpApi/Controllers/FetchUsersController.php index a6f4930..9b3551d 100644 --- a/src/HttpApi/Controllers/FetchUsers.php +++ b/src/HttpApi/Controllers/FetchUsersController.php @@ -8,7 +8,7 @@ use Illuminate\Support\Collection; use Symfony\Component\HttpKernel\Exception\HttpException; use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel; -class FetchUsers extends Controller +class FetchUsersController extends Controller { public function __invoke(Request $request) { diff --git a/src/HttpApi/Controllers/TriggerEvent.php b/src/HttpApi/Controllers/TriggerEventController.php similarity index 95% rename from src/HttpApi/Controllers/TriggerEvent.php rename to src/HttpApi/Controllers/TriggerEventController.php index 78684f3..fa3b19d 100644 --- a/src/HttpApi/Controllers/TriggerEvent.php +++ b/src/HttpApi/Controllers/TriggerEventController.php @@ -6,7 +6,7 @@ use BeyondCode\LaravelWebSockets\Events\ApiMessageSent; use BeyondCode\LaravelWebSockets\HttpApi\Controllers\Controller; use Illuminate\Http\Request; -class TriggerEvent extends Controller +class TriggerEventController extends Controller { public function __invoke(Request $request) { diff --git a/src/Server/OriginCheck.php b/src/Server/OriginCheck.php index 689ef07..883efb7 100644 --- a/src/Server/OriginCheck.php +++ b/src/Server/OriginCheck.php @@ -10,7 +10,6 @@ use Psr\Http\Message\RequestInterface; class OriginCheck implements HttpServerInterface { - use CloseResponseTrait; /** @var \Ratchet\MessageComponentInterface */ diff --git a/src/Server/Router.php b/src/Server/Router.php index 34c73a3..731298e 100644 --- a/src/Server/Router.php +++ b/src/Server/Router.php @@ -3,21 +3,20 @@ namespace BeyondCode\LaravelWebSockets\Server; use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketLogger; -use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannel; -use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannels; -use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchUsers; -use BeyondCode\LaravelWebSockets\HttpApi\Controllers\TriggerEvent; +use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannelController; +use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannelsController; +use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchUsersController; +use BeyondCode\LaravelWebSockets\HttpApi\Controllers\TriggerEventController; use BeyondCode\LaravelWebSockets\WebSockets\Controllers\WebSocketHandler; use Ratchet\WebSocket\MessageComponentInterface; use Ratchet\WebSocket\WsServer; use Symfony\Component\Routing\Route; -use Ratchet\Http\HttpServerInterface; use Symfony\Component\Routing\RouteCollection; use BeyondCode\LaravelWebSockets\Exceptions\InvalidWebSocketController; class Router { - /** @var RouteCollection */ + /** @var \Symfony\Component\Routing\RouteCollection */ protected $routes; public function __construct() @@ -25,13 +24,19 @@ class Router $this->routes = new RouteCollection; } - public function websocket(string $uri, $action) + public function getRoutes(): RouteCollection { - if (!is_subclass_of($action, MessageComponentInterface::class)) { - throw InvalidWebSocketController::withController($action); - } + return $this->routes; + } - $this->get($uri, $action); + public function echo() + { + $this->get('/app/{appKey}', WebSocketHandler::class); + + $this->get('/apps/{appId}/channels', FetchChannelsController::class); + $this->get('/apps/{appId}/channels/{channelName}', FetchChannelController::class); + $this->get('/apps/{appId}/channels/{channelName}/users', FetchUsersController::class); + $this->post('/apps/{appId}/events', TriggerEventController::class); } public function get(string $uri, $action) @@ -59,6 +64,15 @@ class Router $this->addRoute('DELETE', $uri, $action); } + public function websocket(string $uri, $action) + { + if (!is_subclass_of($action, MessageComponentInterface::class)) { + throw InvalidWebSocketController::withController($action); + } + + $this->get($uri, $action); + } + public function addRoute(string $method, string $uri, $action) { $this->routes->add($uri, $this->getRoute($method, $uri, $action)); @@ -66,41 +80,27 @@ class Router protected function getRoute(string $method, string $uri, $action): Route { - return new Route($uri, ['_controller' => $this->wrapAction($action)], [], [], null, [], [$method]); + /** + * If the given action is a class that handles WebSockets, then it's not a regular + * controller but a WebSocketHandler that needs to converted to a WsServer. + * + * If the given action is a regular controller we'll just instanciate it. + */ + $action = is_subclass_of($action, MessageComponentInterface::class) + ? $this->createWebSocketsServer($action) + : app($action); + + return new Route($uri, ['_controller' => $action], [], [], null, [], [$method]); } - public function echo() + protected function createWebSocketsServer(MessageComponentInterface $action): WsServer { - $this->get('/app/{appKey}', WebSocketHandler::class); + $app = app($action); - $this->get('/apps/{appId}/channels', FetchChannels::class); - $this->get('/apps/{appId}/channels/{channelName}', FetchChannel::class); - $this->get('/apps/{appId}/channels/{channelName}/users', FetchUsers::class); - - $this->post('/apps/{appId}/events', TriggerEvent::class); - } - - /** - * @param $action - * @return WsServer|HttpServerInterface - */ - protected function wrapAction($action) - { - if (is_subclass_of($action, MessageComponentInterface::class)) { - $app = app($action); - - if (WebsocketLogger::isEnabled()) { - $app = WebsocketLogger::decorate($app); - } - - return new WsServer($app); + if (WebsocketLogger::isEnabled()) { + $app = WebsocketLogger::decorate($app); } - return app($action); - } - - public function getRoutes(): RouteCollection - { - return $this->routes; + return new WsServer($app); } } \ No newline at end of file