diff --git a/routes/api.php b/routes/api.php new file mode 100644 index 0000000..9b219f8 --- /dev/null +++ b/routes/api.php @@ -0,0 +1,43 @@ +has('socket_id')) { + return response()->json(['error' => 'Socket ID is required'], 400); + } + + if (!request()->has('channel_name')) { + return response()->json(['error' => 'Channel name is required'], 400); + } + + try { + $auth = \Illuminate\Support\Facades\Broadcast::auth(request()); + } catch (\Throwable $e) { + $auth = []; + } + + $auth['socket_id'] = request()->get('socket_id'); + + config(['cache.default' => 'file']); + cache()->remember('socket_' . request()->get('socket_id'), 15, function () { + return [ + 'id' => optional(auth())->id(), + 'type' => optional(auth())->user() + ? get_class(auth()->guard()->user()) + : null, + ]; + }); + + return @$auth; +}); diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index 01d464b..599375e 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -36,15 +36,16 @@ class WebSocketsServiceProvider extends ServiceProvider public function boot() { $this->publishes([ - __DIR__.'/../config/websockets.php' => config_path('websockets.php'), + __DIR__ . '/../config/websockets.php' => config_path('websockets.php'), ], 'config'); $this->mergeConfigFrom( - __DIR__.'/../config/websockets.php', 'websockets' + __DIR__ . '/../config/websockets.php', + 'websockets' ); $this->publishes([ - __DIR__.'/Websocket' => app_path('Websocket') + __DIR__ . '/Websocket' => app_path('Websocket') ]); $this->registerDefaultWebsocketChannels(); @@ -67,6 +68,8 @@ class WebSocketsServiceProvider extends ServiceProvider $this->registerDashboard(); + $this->registerBroadcastAuthRoute(); + $this->registerCommands(); } @@ -123,7 +126,7 @@ class WebSocketsServiceProvider extends ServiceProvider $migrations = (new Finder()) ->files() ->ignoreDotFiles(true) - ->in(__DIR__.'/../database/migrations/sqlite') + ->in(__DIR__ . '/../database/migrations/sqlite') ->name('*.sql'); /** @var SplFileInfo $migration */ @@ -140,11 +143,11 @@ class WebSocketsServiceProvider extends ServiceProvider $this->app->singleton(ConnectionInterface::class, function () { $factory = new MySQLFactory($this->app->make(LoopInterface::class)); - $connectionKey = 'database.connections.'.config('websockets.managers.mysql.connection'); + $connectionKey = 'database.connections.' . config('websockets.managers.mysql.connection'); - $auth = trim(config($connectionKey.'.username').':'.config($connectionKey.'.password'), ':'); - $connection = trim(config($connectionKey.'.host').':'.config($connectionKey.'.port'), ':'); - $database = config($connectionKey.'.database'); + $auth = trim(config($connectionKey . '.username') . ':' . config($connectionKey . '.password'), ':'); + $connection = trim(config($connectionKey . '.host') . ':' . config($connectionKey . '.port'), ':'); + $database = config($connectionKey . '.database'); $database = $factory->createLazyConnection(trim("{$auth}@{$connection}/{$database}", '@')); @@ -183,7 +186,7 @@ class WebSocketsServiceProvider extends ServiceProvider */ protected function registerDashboard() { - $this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets'); + $this->loadViewsFrom(__DIR__ . '/../resources/views/', 'websockets'); $this->registerDashboardRoutes(); $this->registerDashboardGate(); @@ -264,6 +267,19 @@ class WebSocketsServiceProvider extends ServiceProvider }); } + /** + * Register the default broadcasting routes if not already defined. + * + * @return void + */ + protected function registerBroadcastAuthRoute() + { + // If broadcasting/auth route is not defined, load the default routes + if (! Route::has('broadcasting/auth')) { + $this->loadRoutesFrom(__DIR__ . '/../routes/api.php'); + } + } + protected function registerWebSocketHandler() { $this->app->singleton('websockets.handler', function () { diff --git a/src/Websocket/Handler.php b/src/Websocket/Handler.php index 068a673..8f3c0e3 100644 --- a/src/Websocket/Handler.php +++ b/src/Websocket/Handler.php @@ -209,6 +209,10 @@ class Handler implements MessageComponentInterface { $this->authenticateConnection($connection, null); + if (@$connection?->remoteAddress) { + request()->server->set('REMOTE_ADDR', $connection->remoteAddress); + } + // remove connection from $channel_connections foreach ($this->channel_connections as $channel => $connections) { if (in_array($connection->socketId, $connections)) {