1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\Console\Commands; |
4
|
|
|
|
5
|
|
|
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager; |
6
|
|
|
use BeyondCode\LaravelWebSockets\Contracts\StatisticsCollector; |
7
|
|
|
use BeyondCode\LaravelWebSockets\Facades\StatisticsCollector as StatisticsCollectorFacade; |
8
|
|
|
use BeyondCode\LaravelWebSockets\Facades\WebSocketRouter; |
9
|
|
|
use BeyondCode\LaravelWebSockets\Server\Loggers\ConnectionLogger; |
10
|
|
|
use BeyondCode\LaravelWebSockets\Server\Loggers\HttpLogger; |
11
|
|
|
use BeyondCode\LaravelWebSockets\Server\Loggers\WebSocketsLogger; |
12
|
|
|
use BeyondCode\LaravelWebSockets\ServerFactory; |
13
|
|
|
use Illuminate\Console\Command; |
14
|
|
|
use Illuminate\Support\Facades\Cache; |
15
|
|
|
use React\EventLoop\Factory as LoopFactory; |
16
|
|
|
|
17
|
|
|
class StartServer extends Command |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The name and signature of the console command. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $signature = 'websockets:serve |
25
|
|
|
{--host=0.0.0.0} |
26
|
|
|
{--port=6001} |
27
|
|
|
{--disable-statistics : Disable the statistics tracking.} |
28
|
|
|
{--statistics-interval= : The amount of seconds to tick between statistics saving.} |
29
|
|
|
{--debug : Forces the loggers to be enabled and thereby overriding the APP_DEBUG setting.} |
30
|
|
|
{--test : Prepare the server, but do not start it.} |
31
|
|
|
'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The console command description. |
35
|
|
|
* |
36
|
|
|
* @var string|null |
37
|
|
|
*/ |
38
|
|
|
protected $description = 'Start the LaravelWebSockets server.'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the loop instance. |
42
|
|
|
* |
43
|
|
|
* @var \React\EventLoop\LoopInterface |
44
|
|
|
*/ |
45
|
|
|
protected $loop; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The Pusher server instance. |
49
|
|
|
* |
50
|
|
|
* @var \Ratchet\Server\IoServer |
51
|
|
|
*/ |
52
|
|
|
public $server; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Initialize the command. |
56
|
|
|
* |
57
|
|
|
* @return void |
|
|
|
|
58
|
|
|
*/ |
59
|
|
|
public function __construct() |
60
|
|
|
{ |
61
|
|
|
parent::__construct(); |
62
|
|
|
|
63
|
|
|
$this->loop = LoopFactory::create(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Run the command. |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function handle() |
72
|
|
|
{ |
73
|
|
|
$this->configureLoggers(); |
74
|
|
|
|
75
|
|
|
$this->configureManagers(); |
76
|
|
|
|
77
|
|
|
$this->configureStatistics(); |
78
|
|
|
|
79
|
|
|
$this->configureRestartTimer(); |
80
|
|
|
|
81
|
|
|
$this->configureRoutes(); |
82
|
|
|
|
83
|
|
|
$this->startServer(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Configure the loggers used for the console. |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
protected function configureLoggers() |
92
|
|
|
{ |
93
|
|
|
$this->configureHttpLogger(); |
94
|
|
|
$this->configureMessageLogger(); |
95
|
|
|
$this->configureConnectionLogger(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Register the managers that are not resolved |
100
|
|
|
* in the package service provider. |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
protected function configureManagers() |
105
|
|
|
{ |
106
|
|
View Code Duplication |
$this->laravel->singleton(ChannelManager::class, function () { |
|
|
|
|
107
|
|
|
$mode = config('websockets.replication.mode', 'local'); |
108
|
|
|
|
109
|
|
|
$class = config("websockets.replication.modes.{$mode}.channel_manager"); |
110
|
|
|
|
111
|
|
|
return new $class($this->loop); |
112
|
|
|
}); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Register the Statistics Collectors that |
117
|
|
|
* are not resolved in the package service provider. |
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
protected function configureStatistics() |
122
|
|
|
{ |
123
|
|
View Code Duplication |
$this->laravel->singleton(StatisticsCollector::class, function () { |
|
|
|
|
124
|
|
|
$replicationMode = config('websockets.replication.mode', 'local'); |
125
|
|
|
|
126
|
|
|
$class = config("websockets.replication.modes.{$replicationMode}.collector"); |
127
|
|
|
|
128
|
|
|
return new $class; |
129
|
|
|
}); |
130
|
|
|
|
131
|
|
|
if (! $this->option('disable-statistics')) { |
132
|
|
|
$intervalInSeconds = $this->option('statistics-interval') ?: config('websockets.statistics.interval_in_seconds', 3600); |
133
|
|
|
|
134
|
|
|
$this->loop->addPeriodicTimer($intervalInSeconds, function () { |
135
|
|
|
$this->line('Saving statistics...'); |
136
|
|
|
|
137
|
|
|
StatisticsCollectorFacade::save(); |
138
|
|
|
}); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Configure the restart timer. |
144
|
|
|
* |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
public function configureRestartTimer() |
148
|
|
|
{ |
149
|
|
|
$this->lastRestart = $this->getLastRestart(); |
|
|
|
|
150
|
|
|
|
151
|
|
|
$this->loop->addPeriodicTimer(10, function () { |
152
|
|
|
if ($this->getLastRestart() !== $this->lastRestart) { |
153
|
|
|
$this->loop->stop(); |
154
|
|
|
} |
155
|
|
|
}); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Register the routes for the server. |
160
|
|
|
* |
161
|
|
|
* @return void |
162
|
|
|
*/ |
163
|
|
|
protected function configureRoutes() |
164
|
|
|
{ |
165
|
|
|
WebSocketRouter::routes(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Configure the HTTP logger class. |
170
|
|
|
* |
171
|
|
|
* @return void |
172
|
|
|
*/ |
173
|
|
View Code Duplication |
protected function configureHttpLogger() |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
$this->laravel->singleton(HttpLogger::class, function () { |
176
|
|
|
return (new HttpLogger($this->output)) |
177
|
|
|
->enable($this->option('debug') ?: config('app.debug')) |
178
|
|
|
->verbose($this->output->isVerbose()); |
179
|
|
|
}); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Configure the logger for messages. |
184
|
|
|
* |
185
|
|
|
* @return void |
186
|
|
|
*/ |
187
|
|
View Code Duplication |
protected function configureMessageLogger() |
|
|
|
|
188
|
|
|
{ |
189
|
|
|
$this->laravel->singleton(WebSocketsLogger::class, function () { |
190
|
|
|
return (new WebSocketsLogger($this->output)) |
191
|
|
|
->enable($this->option('debug') ?: config('app.debug')) |
192
|
|
|
->verbose($this->output->isVerbose()); |
193
|
|
|
}); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Configure the connection logger. |
198
|
|
|
* |
199
|
|
|
* @return void |
200
|
|
|
*/ |
201
|
|
|
protected function configureConnectionLogger() |
202
|
|
|
{ |
203
|
|
|
$this->laravel->bind(ConnectionLogger::class, function () { |
204
|
|
|
return (new ConnectionLogger($this->output)) |
205
|
|
|
->enable(config('app.debug')) |
206
|
|
|
->verbose($this->output->isVerbose()); |
207
|
|
|
}); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Start the server. |
212
|
|
|
* |
213
|
|
|
* @return void |
214
|
|
|
*/ |
215
|
|
|
protected function startServer() |
216
|
|
|
{ |
217
|
|
|
$this->info("Starting the WebSocket server on port {$this->option('port')}..."); |
218
|
|
|
|
219
|
|
|
$this->buildServer(); |
220
|
|
|
|
221
|
|
|
// For testing, just boot up the server, run it |
222
|
|
|
// but exit after the next tick. |
223
|
|
|
if ($this->option('test')) { |
224
|
|
|
$this->loop->futureTick(function () { |
225
|
|
|
$this->loop->stop(); |
226
|
|
|
}); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
$this->server->run(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Build the server instance. |
234
|
|
|
* |
235
|
|
|
* @return void |
236
|
|
|
*/ |
237
|
|
|
protected function buildServer() |
238
|
|
|
{ |
239
|
|
|
$this->server = new ServerFactory( |
|
|
|
|
240
|
|
|
$this->option('host'), $this->option('port') |
241
|
|
|
); |
242
|
|
|
|
243
|
|
|
$this->server = $this->server |
244
|
|
|
->setLoop($this->loop) |
245
|
|
|
->withRoutes(WebSocketRouter::getRoutes()) |
246
|
|
|
->setConsoleOutput($this->output) |
247
|
|
|
->createServer(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Get the last time the server restarted. |
252
|
|
|
* |
253
|
|
|
* @return int |
254
|
|
|
*/ |
255
|
|
|
protected function getLastRestart() |
256
|
|
|
{ |
257
|
|
|
return Cache::get( |
258
|
|
|
'beyondcode:websockets:restart', 0 |
259
|
|
|
); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.