loop = LoopFactory::create(); } public function handle() { $this ->configureStatisticsLogger() ->configureHttpLogger() ->configureMessageLogger() ->configureConnectionLogger() ->configureRestartTimer() ->registerEchoRoutes() ->registerCustomRoutes() ->configurePubSubReplication() ->startWebSocketServer(); } protected function configureStatisticsLogger() { $connector = new Connector($this->loop, [ 'dns' => $this->getDnsResolver(), 'tls' => config('websockets.statistics.tls'), ]); $browser = new Browser($this->loop, $connector); $this->laravel->singleton(StatisticsLoggerInterface::class, function () use ($browser) { $class = config('websockets.statistics.logger', \BeyondCode\LaravelWebSockets\Statistics\Logger::class); return new $class(app(ChannelManager::class), $browser); }); $this->loop->addPeriodicTimer(config('websockets.statistics.interval_in_seconds'), function () { StatisticsLogger::save(); }); return $this; } protected function configureHttpLogger() { $this->laravel->singleton(HttpLogger::class, function () { return (new HttpLogger($this->output)) ->enable($this->option('debug') ?: config('app.debug')) ->verbose($this->output->isVerbose()); }); return $this; } protected function configureMessageLogger() { $this->laravel->singleton(WebsocketsLogger::class, function () { return (new WebsocketsLogger($this->output)) ->enable($this->option('debug') ?: config('app.debug')) ->verbose($this->output->isVerbose()); }); return $this; } protected function configureConnectionLogger() { $this->laravel->bind(ConnectionLogger::class, function () { return (new ConnectionLogger($this->output)) ->enable(config('app.debug')) ->verbose($this->output->isVerbose()); }); return $this; } public function configureRestartTimer() { $this->lastRestart = $this->getLastRestart(); $this->loop->addPeriodicTimer(10, function () { if ($this->getLastRestart() !== $this->lastRestart) { $this->loop->stop(); } }); return $this; } protected function registerEchoRoutes() { WebSocketsRouter::echo(); return $this; } protected function registerCustomRoutes() { WebSocketsRouter::customRoutes(); return $this; } protected function startWebSocketServer() { $this->info("Starting the WebSocket server on port {$this->option('port')}..."); $routes = WebSocketsRouter::getRoutes(); $server = (new WebSocketServerFactory()) ->setLoop($this->loop) ->useRoutes($routes) ->setHost($this->option('host')) ->setPort($this->option('port')) ->setConsoleOutput($this->output) ->createServer(); if (! $this->option('test')) { /* 🛰 Start the server 🛰 */ $server->run(); } } protected function configurePubSubReplication() { $this->laravel->get(ReplicationInterface::class)->boot($this->loop); return $this; } protected function getDnsResolver(): ResolverInterface { if (! config('websockets.statistics.perform_dns_lookup')) { return new DnsResolver; } $dnsConfig = DnsConfig::loadSystemConfigBlocking(); return (new DnsFactory)->createCached( $dnsConfig->nameservers ? reset($dnsConfig->nameservers) : '1.1.1.1', $this->loop ); } protected function getLastRestart() { return Cache::get('beyondcode:websockets:restart', 0); } }