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