These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace BeyondCode\LaravelWebSockets\Console; |
||
4 | |||
5 | use React\Socket\Connector; |
||
6 | use Clue\React\Buzz\Browser; |
||
7 | use Illuminate\Console\Command; |
||
8 | use React\EventLoop\Factory as LoopFactory; |
||
9 | use BeyondCode\LaravelWebSockets\Statistics\DnsResolver; |
||
10 | use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger; |
||
11 | use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter; |
||
12 | use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger; |
||
13 | use BeyondCode\LaravelWebSockets\Server\WebSocketServerFactory; |
||
14 | use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger; |
||
15 | use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger; |
||
16 | use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; |
||
17 | use BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger; |
||
18 | use BeyondCode\LaravelWebSockets\Statistics\Logger\StatisticsLogger as StatisticsLoggerInterface; |
||
19 | |||
20 | class StartWebSocketServer extends Command |
||
21 | { |
||
22 | protected $signature = 'websockets:serve {--host=0.0.0.0} {--port=6001} '; |
||
23 | |||
24 | protected $description = 'Start the Laravel WebSocket Server'; |
||
25 | |||
26 | /** @var \React\EventLoop\LoopInterface */ |
||
27 | protected $loop; |
||
28 | |||
29 | public function __construct() |
||
30 | { |
||
31 | parent::__construct(); |
||
32 | |||
33 | $this->loop = LoopFactory::create(); |
||
34 | } |
||
35 | |||
36 | public function handle() |
||
37 | { |
||
38 | $this |
||
39 | ->configureStatisticsLogger() |
||
40 | ->configureHttpLogger() |
||
41 | ->configureMessageLogger() |
||
42 | ->configureConnectionLogger() |
||
43 | ->registerEchoRoutes() |
||
44 | ->startWebSocketServer(); |
||
45 | } |
||
46 | |||
47 | protected function configureStatisticsLogger() |
||
48 | { |
||
49 | $connector = new Connector($this->loop, [ |
||
50 | 'dns' => new DnsResolver(), |
||
51 | 'tls' => [ |
||
52 | 'verify_peer' => config('app.env') === 'production', |
||
53 | 'verify_peer_name' => config('app.env') === 'production', |
||
54 | ], |
||
55 | ]); |
||
56 | |||
57 | $browser = new Browser($this->loop, $connector); |
||
58 | |||
59 | app()->singleton(StatisticsLoggerInterface::class, function () use ($browser) { |
||
60 | return new HttpStatisticsLogger(app(ChannelManager::class), $browser); |
||
61 | }); |
||
62 | |||
63 | $this->loop->addPeriodicTimer(config('websockets.statistics.interval_in_seconds'), function () { |
||
64 | StatisticsLogger::save(); |
||
65 | }); |
||
66 | |||
67 | return $this; |
||
68 | } |
||
69 | |||
70 | View Code Duplication | protected function configureHttpLogger() |
|
0 ignored issues
–
show
|
|||
71 | { |
||
72 | app()->singleton(HttpLogger::class, function () { |
||
73 | return (new HttpLogger($this->output)) |
||
74 | ->enable(config('app.debug')) |
||
75 | ->verbose($this->output->isVerbose()); |
||
76 | }); |
||
77 | |||
78 | return $this; |
||
79 | } |
||
80 | |||
81 | View Code Duplication | protected function configureMessageLogger() |
|
82 | { |
||
83 | app()->singleton(WebsocketsLogger::class, function () { |
||
84 | return (new WebsocketsLogger($this->output)) |
||
85 | ->enable(config('app.debug')) |
||
86 | ->verbose($this->output->isVerbose()); |
||
87 | }); |
||
88 | |||
89 | return $this; |
||
90 | } |
||
91 | |||
92 | View Code Duplication | protected function configureConnectionLogger() |
|
93 | { |
||
94 | app()->bind(ConnectionLogger::class, function () { |
||
95 | return (new ConnectionLogger($this->output)) |
||
96 | ->enable(config('app.debug')) |
||
97 | ->verbose($this->output->isVerbose()); |
||
98 | }); |
||
99 | |||
100 | return $this; |
||
101 | } |
||
102 | |||
103 | protected function registerEchoRoutes() |
||
104 | { |
||
105 | WebSocketsRouter::echo(); |
||
106 | |||
107 | return $this; |
||
108 | } |
||
109 | |||
110 | protected function startWebSocketServer() |
||
111 | { |
||
112 | $this->info("Starting the WebSocket server on port {$this->option('port')}..."); |
||
113 | |||
114 | $routes = WebSocketsRouter::getRoutes(); |
||
115 | |||
116 | /* 🛰 Start the server 🛰 */ |
||
117 | (new WebSocketServerFactory()) |
||
118 | ->setLoop($this->loop) |
||
119 | ->useRoutes($routes) |
||
120 | ->setHost($this->option('host')) |
||
121 | ->setPort($this->option('port')) |
||
122 | ->setConsoleOutput($this->output) |
||
123 | ->createServer() |
||
124 | ->run(); |
||
125 | } |
||
126 | } |
||
127 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.