Completed
Push — master ( 53691c...15e6b6 )
by Marcel
01:44
created

src/Console/StartWebSocketServer.php (3 issues)

Upgrade to new PHP Analysis Engine

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\Dns\Config\Config as DnsConfig;
9
use React\EventLoop\Factory as LoopFactory;
10
use React\Dns\Resolver\Factory as DnsFactory;
11
use React\Dns\Resolver\Resolver as ReactDnsResolver;
12
use BeyondCode\LaravelWebSockets\Statistics\DnsResolver;
13
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
14
use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter;
15
use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger;
16
use BeyondCode\LaravelWebSockets\Server\WebSocketServerFactory;
17
use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger;
18
use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger;
19
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
20
use BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger;
21
use BeyondCode\LaravelWebSockets\Statistics\Logger\StatisticsLogger as StatisticsLoggerInterface;
22
23
class StartWebSocketServer extends Command
24
{
25
    protected $signature = 'websockets:serve {--host=0.0.0.0} {--port=6001} {--debug : Forces the loggers to be enabled and thereby overriding the app.debug config setting } ';
26
27
    protected $description = 'Start the Laravel WebSocket Server';
28
29
    /** @var \React\EventLoop\LoopInterface */
30
    protected $loop;
31
32
    public function __construct()
33
    {
34
        parent::__construct();
35
36
        $this->loop = LoopFactory::create();
37
    }
38
39
    public function handle()
40
    {
41
        $this
42
            ->configureStatisticsLogger()
43
            ->configureHttpLogger()
44
            ->configureMessageLogger()
45
            ->configureConnectionLogger()
46
            ->registerEchoRoutes()
47
            ->startWebSocketServer();
48
    }
49
50
    protected function configureStatisticsLogger()
51
    {
52
        $connector = new Connector($this->loop, [
53
            'dns' => $this->getDnsResolver(),
54
            'tls' => [
55
                'verify_peer' => config('app.env') === 'production',
56
                'verify_peer_name' => config('app.env') === 'production',
57
            ],
58
        ]);
59
60
        $browser = new Browser($this->loop, $connector);
61
62
        app()->singleton(StatisticsLoggerInterface::class, function () use ($browser) {
63
            return new HttpStatisticsLogger(app(ChannelManager::class), $browser);
64
        });
65
66
        $this->loop->addPeriodicTimer(config('websockets.statistics.interval_in_seconds'), function () {
67
            StatisticsLogger::save();
68
        });
69
70
        return $this;
71
    }
72
73 View Code Duplication
    protected function configureHttpLogger()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
74
    {
75
        app()->singleton(HttpLogger::class, function () {
76
            return (new HttpLogger($this->output))
77
                ->enable($this->option('debug') ?: config('app.debug'))
78
                ->verbose($this->output->isVerbose());
79
        });
80
81
        return $this;
82
    }
83
84 View Code Duplication
    protected function configureMessageLogger()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
85
    {
86
        app()->singleton(WebsocketsLogger::class, function () {
87
            return (new WebsocketsLogger($this->output))
88
                ->enable($this->option('debug') ?: config('app.debug'))
89
                ->verbose($this->output->isVerbose());
90
        });
91
92
        return $this;
93
    }
94
95 View Code Duplication
    protected function configureConnectionLogger()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
96
    {
97
        app()->bind(ConnectionLogger::class, function () {
98
            return (new ConnectionLogger($this->output))
99
                ->enable(config('app.debug'))
100
                ->verbose($this->output->isVerbose());
101
        });
102
103
        return $this;
104
    }
105
106
    protected function registerEchoRoutes()
107
    {
108
        WebSocketsRouter::echo();
109
110
        return $this;
111
    }
112
113
    protected function startWebSocketServer()
114
    {
115
        $this->info("Starting the WebSocket server on port {$this->option('port')}...");
116
117
        $routes = WebSocketsRouter::getRoutes();
118
119
        /* 🛰 Start the server 🛰  */
120
        (new WebSocketServerFactory())
121
            ->setLoop($this->loop)
122
            ->useRoutes($routes)
123
            ->setHost($this->option('host'))
124
            ->setPort($this->option('port'))
125
            ->setConsoleOutput($this->output)
126
            ->createServer()
127
            ->run();
128
    }
129
130
    protected function getDnsResolver(): ReactDnsResolver
131
    {
132
        if (! config('websockets.statistics.perform_dns_lookup')) {
133
            return new DnsResolver;
134
        }
135
136
        $dnsConfig = DnsConfig::loadSystemConfigBlocking();
137
138
        return (new DnsFactory)->createCached(
139
            $dnsConfig->nameservers
140
                ? reset($dnsConfig->nameservers)
141
                : '1.1.1.1',
142
            $this->loop
143
        );
144
    }
145
}
146