wip
This commit is contained in:
commit
7f0e8727f4
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
@ -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)
|
||||
{
|
||||
|
|
@ -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)
|
||||
{
|
||||
|
|
@ -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)
|
||||
{
|
||||
|
|
@ -10,7 +10,6 @@ use Psr\Http\Message\RequestInterface;
|
|||
|
||||
class OriginCheck implements HttpServerInterface
|
||||
{
|
||||
|
||||
use CloseResponseTrait;
|
||||
|
||||
/** @var \Ratchet\MessageComponentInterface */
|
||||
|
|
|
|||
|
|
@ -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,27 +80,21 @@ class Router
|
|||
|
||||
protected function getRoute(string $method, string $uri, $action): Route
|
||||
{
|
||||
return new Route($uri, ['_controller' => $this->wrapAction($action)], [], [], null, [], [$method]);
|
||||
}
|
||||
|
||||
public function echo()
|
||||
{
|
||||
$this->get('/app/{appKey}', WebSocketHandler::class);
|
||||
|
||||
$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
|
||||
* 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.
|
||||
*/
|
||||
protected function wrapAction($action)
|
||||
$action = is_subclass_of($action, MessageComponentInterface::class)
|
||||
? $this->createWebSocketsServer($action)
|
||||
: app($action);
|
||||
|
||||
return new Route($uri, ['_controller' => $action], [], [], null, [], [$method]);
|
||||
}
|
||||
|
||||
protected function createWebSocketsServer(MessageComponentInterface $action): WsServer
|
||||
{
|
||||
if (is_subclass_of($action, MessageComponentInterface::class)) {
|
||||
$app = app($action);
|
||||
|
||||
if (WebsocketLogger::isEnabled()) {
|
||||
|
|
@ -95,12 +103,4 @@ class Router
|
|||
|
||||
return new WsServer($app);
|
||||
}
|
||||
|
||||
return app($action);
|
||||
}
|
||||
|
||||
public function getRoutes(): RouteCollection
|
||||
{
|
||||
return $this->routes;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue