|
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->startServer(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Configure the loggers used for the console. |
|
86
|
|
|
* |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function configureLoggers() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->configureHttpLogger(); |
|
92
|
|
|
$this->configureMessageLogger(); |
|
93
|
|
|
$this->configureConnectionLogger(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Register the managers that are not resolved |
|
98
|
|
|
* in the package service provider. |
|
99
|
|
|
* |
|
100
|
|
|
* @return void |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function configureManagers() |
|
103
|
|
|
{ |
|
104
|
|
View Code Duplication |
$this->laravel->singleton(ChannelManager::class, function () { |
|
|
|
|
|
|
105
|
|
|
$mode = config('websockets.replication.mode', 'local'); |
|
106
|
|
|
|
|
107
|
|
|
$class = config("websockets.replication.modes.{$mode}.channel_manager"); |
|
108
|
|
|
|
|
109
|
|
|
return new $class($this->loop); |
|
110
|
|
|
}); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Register the Statistics Collectors that |
|
115
|
|
|
* are not resolved in the package service provider. |
|
116
|
|
|
* |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function configureStatistics() |
|
120
|
|
|
{ |
|
121
|
|
View Code Duplication |
$this->laravel->singleton(StatisticsCollector::class, function () { |
|
|
|
|
|
|
122
|
|
|
$replicationMode = config('websockets.replication.mode', 'local'); |
|
123
|
|
|
|
|
124
|
|
|
$class = config("websockets.replication.modes.{$replicationMode}.collector"); |
|
125
|
|
|
|
|
126
|
|
|
return new $class; |
|
127
|
|
|
}); |
|
128
|
|
|
|
|
129
|
|
|
$this->laravel->singleton(StatisticsStore::class, function () { |
|
130
|
|
|
$class = config('websockets.statistics.store'); |
|
131
|
|
|
|
|
132
|
|
|
return new $class; |
|
133
|
|
|
}); |
|
134
|
|
|
|
|
135
|
|
|
if (! $this->option('disable-statistics')) { |
|
136
|
|
|
$intervalInSeconds = $this->option('statistics-interval') ?: config('websockets.statistics.interval_in_seconds', 3600); |
|
137
|
|
|
|
|
138
|
|
|
$this->loop->addPeriodicTimer($intervalInSeconds, function () { |
|
139
|
|
|
$this->line('Saving statistics...'); |
|
140
|
|
|
|
|
141
|
|
|
StatisticsCollectorFacade::save(); |
|
142
|
|
|
}); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Configure the restart timer. |
|
148
|
|
|
* |
|
149
|
|
|
* @return void |
|
150
|
|
|
*/ |
|
151
|
|
|
public function configureRestartTimer() |
|
152
|
|
|
{ |
|
153
|
|
|
$this->lastRestart = $this->getLastRestart(); |
|
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
$this->loop->addPeriodicTimer(10, function () { |
|
156
|
|
|
if ($this->getLastRestart() !== $this->lastRestart) { |
|
157
|
|
|
$this->loop->stop(); |
|
158
|
|
|
} |
|
159
|
|
|
}); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Configure the HTTP logger class. |
|
164
|
|
|
* |
|
165
|
|
|
* @return void |
|
166
|
|
|
*/ |
|
167
|
|
View Code Duplication |
protected function configureHttpLogger() |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
$this->laravel->singleton(HttpLogger::class, function () { |
|
170
|
|
|
return (new HttpLogger($this->output)) |
|
171
|
|
|
->enable($this->option('debug') ?: config('app.debug')) |
|
172
|
|
|
->verbose($this->output->isVerbose()); |
|
173
|
|
|
}); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Configure the logger for messages. |
|
178
|
|
|
* |
|
179
|
|
|
* @return void |
|
180
|
|
|
*/ |
|
181
|
|
View Code Duplication |
protected function configureMessageLogger() |
|
|
|
|
|
|
182
|
|
|
{ |
|
183
|
|
|
$this->laravel->singleton(WebSocketsLogger::class, function () { |
|
184
|
|
|
return (new WebSocketsLogger($this->output)) |
|
185
|
|
|
->enable($this->option('debug') ?: config('app.debug')) |
|
186
|
|
|
->verbose($this->output->isVerbose()); |
|
187
|
|
|
}); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Configure the connection logger. |
|
192
|
|
|
* |
|
193
|
|
|
* @return void |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function configureConnectionLogger() |
|
196
|
|
|
{ |
|
197
|
|
|
$this->laravel->bind(ConnectionLogger::class, function () { |
|
198
|
|
|
return (new ConnectionLogger($this->output)) |
|
199
|
|
|
->enable(config('app.debug')) |
|
200
|
|
|
->verbose($this->output->isVerbose()); |
|
201
|
|
|
}); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Start the server. |
|
206
|
|
|
* |
|
207
|
|
|
* @return void |
|
208
|
|
|
*/ |
|
209
|
|
|
protected function startServer() |
|
210
|
|
|
{ |
|
211
|
|
|
$this->info("Starting the WebSocket server on port {$this->option('port')}..."); |
|
212
|
|
|
|
|
213
|
|
|
$this->buildServer(); |
|
214
|
|
|
|
|
215
|
|
|
// For testing, just boot up the server, run it |
|
216
|
|
|
// but exit after the next tick. |
|
217
|
|
|
if ($this->option('test')) { |
|
218
|
|
|
$this->loop->futureTick(function () { |
|
219
|
|
|
$this->loop->stop(); |
|
220
|
|
|
}); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
$this->server->run(); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Build the server instance. |
|
228
|
|
|
* |
|
229
|
|
|
* @return void |
|
230
|
|
|
*/ |
|
231
|
|
|
protected function buildServer() |
|
232
|
|
|
{ |
|
233
|
|
|
$this->server = new ServerFactory( |
|
|
|
|
|
|
234
|
|
|
$this->option('host'), $this->option('port') |
|
235
|
|
|
); |
|
236
|
|
|
|
|
237
|
|
|
$this->server = $this->server |
|
238
|
|
|
->setLoop($this->loop) |
|
239
|
|
|
->withRoutes(WebSocketRouter::getRoutes()) |
|
240
|
|
|
->setConsoleOutput($this->output) |
|
241
|
|
|
->createServer(); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Get the last time the server restarted. |
|
246
|
|
|
* |
|
247
|
|
|
* @return int |
|
248
|
|
|
*/ |
|
249
|
|
|
protected function getLastRestart() |
|
250
|
|
|
{ |
|
251
|
|
|
return Cache::get( |
|
252
|
|
|
'beyondcode:websockets:restart', 0 |
|
253
|
|
|
); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
Adding a
@returnannotation 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.