diff --git a/src/Servers/Reverb/Controller.php b/src/Servers/Reverb/Controller.php index 350bbfaf..f4428a12 100644 --- a/src/Servers/Reverb/Controller.php +++ b/src/Servers/Reverb/Controller.php @@ -11,6 +11,11 @@ class Controller { + public function __construct(protected Server $server, protected ApplicationProvider $applications) + { + // + } + /** * Invoke the Reverb WebSocket server. */ @@ -20,11 +25,10 @@ public function __invoke(RequestInterface $request, WsConnection $connection, st return; } - $server = app(Server::class); - $server->open($reverbConnection); + $this->server->open($reverbConnection); - $connection->on('message', fn (string $message) => $server->message($reverbConnection, $message)); - $connection->on('close', fn () => $server->close($reverbConnection)); + $connection->on('message', fn (string $message) => $this->server->message($reverbConnection, $message)); + $connection->on('close', fn () => $this->server->close($reverbConnection)); } /** @@ -33,7 +37,7 @@ public function __invoke(RequestInterface $request, WsConnection $connection, st protected function connection(RequestInterface $request, WsConnection $connection, string $key): ?ReverbConnection { try { - $application = app(ApplicationProvider::class)->findByKey($key); + $application = $this->applications->findByKey($key); } catch (InvalidApplication $e) { $connection->send('{"event":"pusher:error","data":"{\"code\":4001,\"message\":\"Application does not exist\"}"}'); diff --git a/src/Servers/Reverb/Factory.php b/src/Servers/Reverb/Factory.php index c70ef2be..59c59b9f 100644 --- a/src/Servers/Reverb/Factory.php +++ b/src/Servers/Reverb/Factory.php @@ -2,6 +2,7 @@ namespace Laravel\Reverb\Servers\Reverb; +use Laravel\Reverb\Contracts\ApplicationProvider; use Laravel\Reverb\Http\Route; use Laravel\Reverb\Http\Router; use Laravel\Reverb\Http\Server as HttpServer; @@ -11,6 +12,7 @@ use Laravel\Reverb\Pusher\Http\Controllers\EventsBatchController; use Laravel\Reverb\Pusher\Http\Controllers\EventsController; use Laravel\Reverb\Pusher\Http\Controllers\UsersTerminateController; +use Laravel\Reverb\Server; use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Socket\SocketServer; @@ -39,7 +41,7 @@ protected static function routes(): RouteCollection { $routes = new RouteCollection; - $routes->add('sockets', Route::get('/app/{appKey}', new Controller)); + $routes->add('sockets', Route::get('/app/{appKey}', new Controller(app(Server::class), app(ApplicationProvider::class)))); $routes->add('events', Route::post('/apps/{appId}/events', new EventsController)); $routes->add('events_batch', Route::post('/apps/{appId}/batch_events', new EventsBatchController)); $routes->add('channels', Route::get('/apps/{appId}/channels', new ChannelsController));