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