134 lines
5.0 KiB
PHP
134 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace BeyondCode\LaravelWebSockets;
|
|
|
|
use BeyondCode\LaravelWebSockets\Apps\AppProvider;
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard;
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\DashboardApiController;
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage;
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard;
|
|
use BeyondCode\LaravelWebSockets\PubSub\Broadcasters\RedisPusherBroadcaster;
|
|
use BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient;
|
|
use BeyondCode\LaravelWebSockets\PubSub\Drivers\RedisClient;
|
|
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
|
|
use BeyondCode\LaravelWebSockets\Server\Router;
|
|
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController;
|
|
use BeyondCode\LaravelWebSockets\Statistics\Http\Middleware\Authorize as AuthorizeStatistics;
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager;
|
|
use Pusher\Pusher;
|
|
use Psr\Log\LoggerInterface;
|
|
use Illuminate\Broadcasting\BroadcastManager;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class WebSocketsServiceProvider extends ServiceProvider
|
|
{
|
|
public function boot()
|
|
{
|
|
$this->publishes([
|
|
__DIR__.'/../config/websockets.php' => base_path('config/websockets.php'),
|
|
], 'config');
|
|
|
|
if (! Schema::hasTable('websockets_statistics_entries')) {
|
|
$this->publishes([
|
|
__DIR__.'/../database/migrations/create_websockets_statistics_entries_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_websockets_statistics_entries_table.php'),
|
|
], 'migrations');
|
|
}
|
|
|
|
$this
|
|
->registerRoutes()
|
|
->registerDashboardGate();
|
|
|
|
$this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets');
|
|
|
|
$this->commands([
|
|
Console\StartWebSocketServer::class,
|
|
Console\CleanStatistics::class,
|
|
]);
|
|
|
|
$this->configurePubSub();
|
|
}
|
|
|
|
protected function configurePubSub()
|
|
{
|
|
if (config('websockets.replication.enabled') !== true || config('websockets.replication.driver') !== 'redis') {
|
|
$this->app->singleton(ReplicationInterface::class, function () {
|
|
return new LocalClient();
|
|
});
|
|
|
|
return;
|
|
}
|
|
|
|
$this->app->singleton(ReplicationInterface::class, function () {
|
|
return (new RedisClient())->boot($this->loop);
|
|
});
|
|
|
|
$this->app->get(BroadcastManager::class)->extend('redis-pusher', function ($app, array $config) {
|
|
$pusher = new Pusher(
|
|
$config['key'], $config['secret'],
|
|
$config['app_id'], $config['options'] ?? []
|
|
);
|
|
|
|
if ($config['log'] ?? false) {
|
|
$pusher->setLogger($this->app->make(LoggerInterface::class));
|
|
}
|
|
|
|
return new RedisPusherBroadcaster(
|
|
$pusher,
|
|
$config['app_id'],
|
|
$this->app->make('redis'),
|
|
$config['connection'] ?? null
|
|
);
|
|
});
|
|
}
|
|
|
|
public function register()
|
|
{
|
|
$this->mergeConfigFrom(__DIR__.'/../config/websockets.php', 'websockets');
|
|
|
|
$this->app->singleton('websockets.router', function () {
|
|
return new Router();
|
|
});
|
|
|
|
$this->app->singleton(ChannelManager::class, function () {
|
|
return config('websockets.channel_manager') !== null && class_exists(config('websockets.channel_manager'))
|
|
? $this->app->make(config('websockets.channel_manager')) : new ArrayChannelManager();
|
|
});
|
|
|
|
$this->app->singleton(AppProvider::class, function () {
|
|
return $this->app->make(config('websockets.app_provider'));
|
|
});
|
|
}
|
|
|
|
protected function registerRoutes()
|
|
{
|
|
Route::prefix(config('websockets.path'))->group(function () {
|
|
Route::middleware(config('websockets.middleware', [AuthorizeDashboard::class]))->group(function () {
|
|
Route::get('/', ShowDashboard::class);
|
|
Route::get('/api/{appId}/statistics', [DashboardApiController::class, 'getStatistics']);
|
|
Route::post('auth', AuthenticateDashboard::class);
|
|
Route::post('event', SendMessage::class);
|
|
});
|
|
|
|
Route::middleware(AuthorizeStatistics::class)->group(function () {
|
|
Route::post('statistics', [WebSocketStatisticsEntriesController::class, 'store']);
|
|
});
|
|
});
|
|
|
|
return $this;
|
|
}
|
|
|
|
protected function registerDashboardGate()
|
|
{
|
|
Gate::define('viewWebSocketsDashboard', function ($user = null) {
|
|
return $this->app->environment('local');
|
|
});
|
|
|
|
return $this;
|
|
}
|
|
}
|