2018-11-20 10:32:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace BeyondCode\LaravelWebSockets;
|
|
|
|
|
|
2020-08-13 11:51:18 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Apps\AppManager;
|
2018-12-04 21:22:33 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\DashboardApiController;
|
2020-03-04 09:58:39 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
|
2018-12-04 09:25:34 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard;
|
2020-08-13 06:59:01 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\PubSub\Broadcasters\RedisPusherBroadcaster;
|
2020-03-04 09:58:39 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Server\Router;
|
2020-08-18 18:22:29 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
|
2018-12-04 21:22:33 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
2018-12-10 19:47:52 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager;
|
2020-08-13 07:20:31 +00:00
|
|
|
use Illuminate\Broadcasting\BroadcastManager;
|
2020-03-04 09:58:39 +00:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
|
use Illuminate\Support\Facades\Route;
|
2020-08-13 11:02:58 +00:00
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use Pusher\Pusher;
|
2018-11-20 10:32:56 +00:00
|
|
|
|
2018-11-25 23:40:32 +00:00
|
|
|
class WebSocketsServiceProvider extends ServiceProvider
|
2018-11-20 10:32:56 +00:00
|
|
|
{
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Boot the service provider.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2019-07-28 18:50:10 +00:00
|
|
|
public function boot()
|
2018-11-20 10:32:56 +00:00
|
|
|
{
|
2018-11-22 22:55:39 +00:00
|
|
|
$this->publishes([
|
2019-07-28 19:29:16 +00:00
|
|
|
__DIR__.'/../config/websockets.php' => base_path('config/websockets.php'),
|
2018-11-22 22:55:39 +00:00
|
|
|
], 'config');
|
|
|
|
|
|
2020-08-13 16:28:57 +00:00
|
|
|
$this->publishes([
|
2020-08-13 16:51:09 +00:00
|
|
|
__DIR__.'/../database/migrations/0000_00_00_000000_create_websockets_statistics_entries_table.php' => database_path('migrations/0000_00_00_000000_create_websockets_statistics_entries_table.php'),
|
2020-08-13 16:28:57 +00:00
|
|
|
], 'migrations');
|
2018-12-03 10:58:42 +00:00
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
$this->registerDashboardRoutes()
|
2018-12-03 23:05:05 +00:00
|
|
|
->registerDashboardGate();
|
2018-11-23 23:06:28 +00:00
|
|
|
|
2019-07-28 19:29:16 +00:00
|
|
|
$this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets');
|
2018-11-23 23:06:28 +00:00
|
|
|
|
2018-11-20 10:51:00 +00:00
|
|
|
$this->commands([
|
|
|
|
|
Console\StartWebSocketServer::class,
|
2018-12-04 10:48:07 +00:00
|
|
|
Console\CleanStatistics::class,
|
2019-11-06 08:25:55 +00:00
|
|
|
Console\RestartWebSocketServer::class,
|
2018-11-20 10:51:00 +00:00
|
|
|
]);
|
2019-03-25 22:00:54 +00:00
|
|
|
|
2019-07-28 18:50:10 +00:00
|
|
|
$this->configurePubSub();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Register the service provider.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
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 () {
|
|
|
|
|
$channelManager = config('websockets.managers.channel', ArrayChannelManager::class);
|
|
|
|
|
|
|
|
|
|
return new $channelManager;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->app->singleton(AppManager::class, function () {
|
|
|
|
|
return $this->app->make(config('websockets.managers.app'));
|
|
|
|
|
});
|
2020-08-18 18:22:29 +00:00
|
|
|
|
|
|
|
|
$this->app->singleton(StatisticsDriver::class, function () {
|
|
|
|
|
$driver = config('websockets.statistics.driver');
|
|
|
|
|
|
|
|
|
|
return $this->app->make(
|
|
|
|
|
config('websockets.statistics')[$driver]['driver']
|
|
|
|
|
??
|
|
|
|
|
\BeyondCode\LaravelWebSockets\Statistics\Drivers\DatabaseDriver::class
|
|
|
|
|
);
|
|
|
|
|
});
|
2020-08-18 17:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Configure the PubSub replication.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2019-07-28 18:50:10 +00:00
|
|
|
protected function configurePubSub()
|
|
|
|
|
{
|
2020-08-13 19:05:57 +00:00
|
|
|
$this->app->make(BroadcastManager::class)->extend('websockets', function ($app, array $config) {
|
2019-03-25 22:00:54 +00:00
|
|
|
$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
|
|
|
|
|
);
|
|
|
|
|
});
|
2018-11-20 10:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Register the dashboard routes.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function registerDashboardRoutes()
|
2018-11-26 22:38:27 +00:00
|
|
|
{
|
2020-08-13 10:22:58 +00:00
|
|
|
Route::prefix(config('websockets.dashboard.path'))->group(function () {
|
|
|
|
|
Route::middleware(config('websockets.dashboard.middleware', [AuthorizeDashboard::class]))->group(function () {
|
2018-12-04 09:11:53 +00:00
|
|
|
Route::get('/', ShowDashboard::class);
|
2019-07-28 18:50:10 +00:00
|
|
|
Route::get('/api/{appId}/statistics', [DashboardApiController::class, 'getStatistics']);
|
2018-12-04 09:11:53 +00:00
|
|
|
Route::post('auth', AuthenticateDashboard::class);
|
|
|
|
|
Route::post('event', SendMessage::class);
|
|
|
|
|
});
|
2018-11-26 22:38:27 +00:00
|
|
|
});
|
2018-12-03 23:05:05 +00:00
|
|
|
|
|
|
|
|
return $this;
|
2018-11-26 22:38:27 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Register the dashboard gate.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-11-24 22:52:55 +00:00
|
|
|
protected function registerDashboardGate()
|
|
|
|
|
{
|
2018-11-25 23:57:22 +00:00
|
|
|
Gate::define('viewWebSocketsDashboard', function ($user = null) {
|
2019-07-28 19:33:30 +00:00
|
|
|
return $this->app->environment('local');
|
2018-11-23 23:06:28 +00:00
|
|
|
});
|
2018-12-03 23:05:05 +00:00
|
|
|
|
|
|
|
|
return $this;
|
2018-11-20 10:32:56 +00:00
|
|
|
}
|
|
|
|
|
}
|