|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets; |
|
4
|
|
|
|
|
5
|
|
|
use Pusher\Pusher; |
|
6
|
|
|
use Psr\Log\LoggerInterface; |
|
7
|
|
|
use Illuminate\Support\Facades\Gate; |
|
8
|
|
|
use Illuminate\Support\Facades\Route; |
|
9
|
|
|
use Illuminate\Support\ServiceProvider; |
|
10
|
|
|
use Illuminate\Broadcasting\BroadcastManager; |
|
11
|
|
|
use BeyondCode\LaravelWebSockets\Server\Router; |
|
12
|
|
|
use BeyondCode\LaravelWebSockets\Apps\AppProvider; |
|
13
|
|
|
use BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient; |
|
14
|
|
|
use BeyondCode\LaravelWebSockets\PubSub\Drivers\RedisClient; |
|
15
|
|
|
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface; |
|
16
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; |
|
17
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage; |
|
18
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard; |
|
19
|
|
|
use BeyondCode\LaravelWebSockets\PubSub\Broadcasters\RedisPusherBroadcaster; |
|
20
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard; |
|
21
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\DashboardApiController; |
|
22
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager; |
|
23
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard; |
|
24
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Http\Middleware\Authorize as AuthorizeStatistics; |
|
25
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController; |
|
26
|
|
|
|
|
27
|
|
|
class WebSocketsServiceProvider extends ServiceProvider |
|
28
|
|
|
{ |
|
29
|
|
|
public function boot() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->publishes([ |
|
32
|
|
|
__DIR__.'/../config/websockets.php' => base_path('config/websockets.php'), |
|
33
|
|
|
], 'config'); |
|
34
|
|
|
|
|
35
|
|
|
if (! class_exists('CreateWebSocketsStatisticsEntries')) { |
|
36
|
|
|
$this->publishes([ |
|
37
|
|
|
__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'), |
|
38
|
|
|
], 'migrations'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$this |
|
42
|
|
|
->registerRoutes() |
|
43
|
|
|
->registerDashboardGate(); |
|
44
|
|
|
|
|
45
|
|
|
$this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets'); |
|
46
|
|
|
|
|
47
|
|
|
$this->commands([ |
|
48
|
|
|
Console\StartWebSocketServer::class, |
|
49
|
|
|
Console\CleanStatistics::class, |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
|
|
$this->configurePubSub(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function configurePubSub() |
|
56
|
|
|
{ |
|
57
|
|
|
if (config('websockets.replication.enabled') !== true || config('websockets.replication.driver') !== 'redis') { |
|
58
|
|
|
$this->app->singleton(ReplicationInterface::class, function () { |
|
59
|
|
|
return new LocalClient(); |
|
60
|
|
|
}); |
|
61
|
|
|
|
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->app->singleton(ReplicationInterface::class, function () { |
|
66
|
|
|
return (new RedisClient())->boot($this->loop); |
|
|
|
|
|
|
67
|
|
|
}); |
|
68
|
|
|
|
|
69
|
|
|
$this->app->get(BroadcastManager::class)->extend('redis-pusher', function ($app, array $config) { |
|
70
|
|
|
$pusher = new Pusher( |
|
71
|
|
|
$config['key'], $config['secret'], |
|
72
|
|
|
$config['app_id'], $config['options'] ?? [] |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
if ($config['log'] ?? false) { |
|
76
|
|
|
$pusher->setLogger($this->app->make(LoggerInterface::class)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return new RedisPusherBroadcaster( |
|
80
|
|
|
$pusher, |
|
81
|
|
|
$config['app_id'], |
|
82
|
|
|
$this->app->make('redis'), |
|
83
|
|
|
$config['connection'] ?? null |
|
84
|
|
|
); |
|
85
|
|
|
}); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function register() |
|
89
|
|
|
{ |
|
90
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/websockets.php', 'websockets'); |
|
91
|
|
|
|
|
92
|
|
|
$this->app->singleton('websockets.router', function () { |
|
93
|
|
|
return new Router(); |
|
94
|
|
|
}); |
|
95
|
|
|
|
|
96
|
|
|
$this->app->singleton(ChannelManager::class, function () { |
|
97
|
|
|
return config('websockets.channel_manager') !== null && class_exists(config('websockets.channel_manager')) |
|
98
|
|
|
? $this->app->make(config('websockets.channel_manager')) : new ArrayChannelManager(); |
|
99
|
|
|
}); |
|
100
|
|
|
|
|
101
|
|
|
$this->app->singleton(AppProvider::class, function () { |
|
102
|
|
|
return $this->app->make(config('websockets.app_provider')); |
|
103
|
|
|
}); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
protected function registerRoutes() |
|
107
|
|
|
{ |
|
108
|
|
|
Route::prefix(config('websockets.path'))->group(function () { |
|
109
|
|
|
Route::middleware(config('websockets.middleware', [AuthorizeDashboard::class]))->group(function () { |
|
110
|
|
|
Route::get('/', ShowDashboard::class); |
|
111
|
|
|
Route::get('/api/{appId}/statistics', [DashboardApiController::class, 'getStatistics']); |
|
112
|
|
|
Route::post('auth', AuthenticateDashboard::class); |
|
113
|
|
|
Route::post('event', SendMessage::class); |
|
114
|
|
|
}); |
|
115
|
|
|
|
|
116
|
|
|
Route::middleware(AuthorizeStatistics::class)->group(function () { |
|
117
|
|
|
Route::post('statistics', [WebSocketStatisticsEntriesController::class, 'store']); |
|
118
|
|
|
}); |
|
119
|
|
|
}); |
|
120
|
|
|
|
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
protected function registerDashboardGate() |
|
125
|
|
|
{ |
|
126
|
|
|
Gate::define('viewWebSocketsDashboard', function ($user = null) { |
|
|
|
|
|
|
127
|
|
|
return $this->app->environment('local'); |
|
128
|
|
|
}); |
|
129
|
|
|
|
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: