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()
|
public function boot()
|
||||||
{
|
{
|
||||||
$this->publishes([
|
$this->publishes([
|
||||||
__DIR__.'/../config/websockets.php' => config_path('websockets.php'),
|
__DIR__ . '/../config/websockets.php' => config_path('websockets.php'),
|
||||||
], 'config');
|
], 'config');
|
||||||
|
|
||||||
$this->mergeConfigFrom(
|
$this->mergeConfigFrom(
|
||||||
__DIR__.'/../config/websockets.php', 'websockets'
|
__DIR__ . '/../config/websockets.php',
|
||||||
|
'websockets'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->publishes([
|
$this->publishes([
|
||||||
__DIR__.'/Websocket' => app_path('Websocket')
|
__DIR__ . '/Websocket' => app_path('Websocket')
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->registerDefaultWebsocketChannels();
|
$this->registerDefaultWebsocketChannels();
|
||||||
|
|
@ -67,6 +68,8 @@ class WebSocketsServiceProvider extends ServiceProvider
|
||||||
|
|
||||||
$this->registerDashboard();
|
$this->registerDashboard();
|
||||||
|
|
||||||
|
$this->registerBroadcastAuthRoute();
|
||||||
|
|
||||||
$this->registerCommands();
|
$this->registerCommands();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -123,7 +126,7 @@ class WebSocketsServiceProvider extends ServiceProvider
|
||||||
$migrations = (new Finder())
|
$migrations = (new Finder())
|
||||||
->files()
|
->files()
|
||||||
->ignoreDotFiles(true)
|
->ignoreDotFiles(true)
|
||||||
->in(__DIR__.'/../database/migrations/sqlite')
|
->in(__DIR__ . '/../database/migrations/sqlite')
|
||||||
->name('*.sql');
|
->name('*.sql');
|
||||||
|
|
||||||
/** @var SplFileInfo $migration */
|
/** @var SplFileInfo $migration */
|
||||||
|
|
@ -140,11 +143,11 @@ class WebSocketsServiceProvider extends ServiceProvider
|
||||||
$this->app->singleton(ConnectionInterface::class, function () {
|
$this->app->singleton(ConnectionInterface::class, function () {
|
||||||
$factory = new MySQLFactory($this->app->make(LoopInterface::class));
|
$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'), ':');
|
$auth = trim(config($connectionKey . '.username') . ':' . config($connectionKey . '.password'), ':');
|
||||||
$connection = trim(config($connectionKey.'.host').':'.config($connectionKey.'.port'), ':');
|
$connection = trim(config($connectionKey . '.host') . ':' . config($connectionKey . '.port'), ':');
|
||||||
$database = config($connectionKey.'.database');
|
$database = config($connectionKey . '.database');
|
||||||
|
|
||||||
$database = $factory->createLazyConnection(trim("{$auth}@{$connection}/{$database}", '@'));
|
$database = $factory->createLazyConnection(trim("{$auth}@{$connection}/{$database}", '@'));
|
||||||
|
|
||||||
|
|
@ -183,7 +186,7 @@ class WebSocketsServiceProvider extends ServiceProvider
|
||||||
*/
|
*/
|
||||||
protected function registerDashboard()
|
protected function registerDashboard()
|
||||||
{
|
{
|
||||||
$this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets');
|
$this->loadViewsFrom(__DIR__ . '/../resources/views/', 'websockets');
|
||||||
|
|
||||||
$this->registerDashboardRoutes();
|
$this->registerDashboardRoutes();
|
||||||
$this->registerDashboardGate();
|
$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()
|
protected function registerWebSocketHandler()
|
||||||
{
|
{
|
||||||
$this->app->singleton('websockets.handler', function () {
|
$this->app->singleton('websockets.handler', function () {
|
||||||
|
|
|
||||||
|
|
@ -209,6 +209,10 @@ class Handler implements MessageComponentInterface
|
||||||
{
|
{
|
||||||
$this->authenticateConnection($connection, null);
|
$this->authenticateConnection($connection, null);
|
||||||
|
|
||||||
|
if (@$connection?->remoteAddress) {
|
||||||
|
request()->server->set('REMOTE_ADDR', $connection->remoteAddress);
|
||||||
|
}
|
||||||
|
|
||||||
// remove connection from $channel_connections
|
// remove connection from $channel_connections
|
||||||
foreach ($this->channel_connections as $channel => $connections) {
|
foreach ($this->channel_connections as $channel => $connections) {
|
||||||
if (in_array($connection->socketId, $connections)) {
|
if (in_array($connection->socketId, $connections)) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue