1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets; |
4
|
|
|
|
5
|
|
|
use BeyondCode\LaravelWebSockets\Apps\AppManager; |
6
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard; |
7
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage; |
8
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard; |
9
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowStatistics; |
10
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard; |
11
|
|
|
use BeyondCode\LaravelWebSockets\PubSub\Broadcasters\RedisPusherBroadcaster; |
12
|
|
|
use BeyondCode\LaravelWebSockets\Server\Router; |
13
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver; |
14
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; |
15
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager; |
16
|
|
|
use Illuminate\Broadcasting\BroadcastManager; |
17
|
|
|
use Illuminate\Support\Facades\Gate; |
18
|
|
|
use Illuminate\Support\Facades\Route; |
19
|
|
|
use Illuminate\Support\ServiceProvider; |
20
|
|
|
use Psr\Log\LoggerInterface; |
21
|
|
|
use Pusher\Pusher; |
22
|
|
|
|
23
|
|
|
class WebSocketsServiceProvider extends ServiceProvider |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Boot the service provider. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function boot() |
31
|
|
|
{ |
32
|
|
|
$this->publishes([ |
33
|
|
|
__DIR__.'/../config/websockets.php' => base_path('config/websockets.php'), |
34
|
|
|
], 'config'); |
35
|
|
|
|
36
|
|
|
$this->publishes([ |
37
|
|
|
__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'), |
38
|
|
|
], 'migrations'); |
39
|
|
|
|
40
|
|
|
$this->registerDashboardRoutes() |
41
|
|
|
->registerDashboardGate(); |
42
|
|
|
|
43
|
|
|
$this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets'); |
44
|
|
|
|
45
|
|
|
$this->commands([ |
46
|
|
|
Console\StartWebSocketServer::class, |
47
|
|
|
Console\CleanStatistics::class, |
48
|
|
|
Console\RestartWebSocketServer::class, |
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
$this->configurePubSub(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Register the service provider. |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function register() |
60
|
|
|
{ |
61
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/websockets.php', 'websockets'); |
62
|
|
|
|
63
|
|
|
$this->app->singleton('websockets.router', function () { |
64
|
|
|
return new Router(); |
65
|
|
|
}); |
66
|
|
|
|
67
|
|
|
$this->app->singleton(ChannelManager::class, function () { |
68
|
|
|
$channelManager = config('websockets.managers.channel', ArrayChannelManager::class); |
69
|
|
|
|
70
|
|
|
return new $channelManager; |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
$this->app->singleton(AppManager::class, function () { |
74
|
|
|
return $this->app->make(config('websockets.managers.app')); |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
$this->app->singleton(StatisticsDriver::class, function () { |
78
|
|
|
$driver = config('websockets.statistics.driver'); |
79
|
|
|
|
80
|
|
|
return $this->app->make( |
81
|
|
|
config('websockets.statistics')[$driver]['driver'] |
82
|
|
|
?? |
83
|
|
|
\BeyondCode\LaravelWebSockets\Statistics\Drivers\DatabaseDriver::class |
84
|
|
|
); |
85
|
|
|
}); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Configure the PubSub replication. |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
protected function configurePubSub() |
94
|
|
|
{ |
95
|
|
|
$this->app->make(BroadcastManager::class)->extend('websockets', function ($app, array $config) { |
96
|
|
|
$pusher = new Pusher( |
97
|
|
|
$config['key'], $config['secret'], |
98
|
|
|
$config['app_id'], $config['options'] ?? [] |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
if ($config['log'] ?? false) { |
102
|
|
|
$pusher->setLogger($this->app->make(LoggerInterface::class)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return new RedisPusherBroadcaster( |
106
|
|
|
$pusher, |
107
|
|
|
$config['app_id'], |
108
|
|
|
$this->app->make('redis'), |
109
|
|
|
$config['connection'] ?? null |
110
|
|
|
); |
111
|
|
|
}); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Register the dashboard routes. |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
protected function registerDashboardRoutes() |
120
|
|
|
{ |
121
|
|
|
Route::group([ |
122
|
|
|
'prefix' => config('websockets.dashboard.path'), |
123
|
|
|
'as' => 'laravel-websockets.', |
124
|
|
|
'middleware' => config('websockets.dashboard.middleware', [AuthorizeDashboard::class]), |
125
|
|
|
], function () { |
126
|
|
|
Route::get('/', ShowDashboard::class)->name('dashboard'); |
127
|
|
|
Route::get('/api/{appId}/statistics', ShowStatistics::class)->name('statistics'); |
128
|
|
|
Route::post('/auth', AuthenticateDashboard::class)->name('auth'); |
129
|
|
|
Route::post('/event', SendMessage::class)->name('event'); |
130
|
|
|
}); |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Register the dashboard gate. |
137
|
|
|
* |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
protected function registerDashboardGate() |
141
|
|
|
{ |
142
|
|
|
Gate::define('viewWebSocketsDashboard', function ($user = null) { |
|
|
|
|
143
|
|
|
return $this->app->environment('local'); |
144
|
|
|
}); |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.