A default broadcasting route, I ip on close
This commit is contained in:
parent
7deba919b1
commit
55c819700b
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Broadcasting Routes
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Here are the default broadcasting routes for Laravel WebSockets.
|
||||
| Users can override these by defining their own routes with the same names.
|
||||
|
|
||||
*/
|
||||
|
||||
Route::any('broadcasting/auth', function () {
|
||||
if (!request()->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;
|
||||
});
|
||||
|
|
@ -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 () {
|
||||
|
|
|
|||
|
|
@ -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)) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue