2018-11-20 10:51:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-11-21 11:13:40 +00:00
|
|
|
namespace BeyondCode\LaravelWebSockets\Console;
|
2018-11-20 10:51:00 +00:00
|
|
|
|
2018-12-03 12:33:20 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
|
2018-11-30 14:51:30 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter;
|
2018-11-24 14:23:59 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger;
|
2018-11-27 20:30:33 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger;
|
2018-11-30 14:52:11 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger;
|
2018-12-03 13:35:00 +00:00
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
use GuzzleHttp\HandlerStack;
|
2018-11-20 10:51:00 +00:00
|
|
|
use Illuminate\Console\Command;
|
2018-11-27 20:46:03 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Server\WebSocketServerFactory;
|
2018-11-20 10:51:00 +00:00
|
|
|
|
|
|
|
|
use React\EventLoop\Factory as LoopFactory;
|
2018-12-03 13:35:00 +00:00
|
|
|
use WyriHaximus\React\GuzzlePsr7\HttpClientAdapter;
|
2018-11-20 10:51:00 +00:00
|
|
|
|
|
|
|
|
class StartWebSocketServer extends Command
|
|
|
|
|
{
|
2018-11-29 16:29:55 +00:00
|
|
|
protected $signature = 'websockets:serve {--host=0.0.0.0} {--port=6001} ';
|
2018-11-20 10:51:00 +00:00
|
|
|
|
|
|
|
|
protected $description = 'Start the Laravel WebSocket Server';
|
|
|
|
|
|
2018-12-03 12:33:20 +00:00
|
|
|
/** @var \React\EventLoop\LoopInterface */
|
|
|
|
|
protected $loop;
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
|
|
$this->loop = LoopFactory::create();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 10:51:00 +00:00
|
|
|
public function handle()
|
|
|
|
|
{
|
2018-11-24 00:25:40 +00:00
|
|
|
$this
|
2018-12-03 12:33:20 +00:00
|
|
|
->configureStatisticsLogger()
|
2018-11-27 20:30:33 +00:00
|
|
|
->configureHttpLogger()
|
2018-11-24 14:23:59 +00:00
|
|
|
->configureMessageLogger()
|
|
|
|
|
->configureConnectionLogger()
|
2018-11-24 00:25:40 +00:00
|
|
|
->registerEchoRoutes()
|
|
|
|
|
->startWebSocketServer();
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 13:46:51 +00:00
|
|
|
protected function configureStatisticsLogger()
|
|
|
|
|
{
|
|
|
|
|
$handler = new HttpClientAdapter($this->loop);
|
|
|
|
|
|
|
|
|
|
$client = new Client([
|
|
|
|
|
'handler' => HandlerStack::create($handler),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
app()->singleton('websockets.statisticslogger', function() use ($client) {
|
|
|
|
|
return new StatisticsLogger(app(ChannelManager::class, $client));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->loop->addPeriodicTimer(60, function() {
|
|
|
|
|
StatisticsLogger::save($this->loop);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-27 20:30:33 +00:00
|
|
|
protected function configureHttpLogger()
|
|
|
|
|
{
|
|
|
|
|
app()->singleton(HttpLogger::class, function() {
|
|
|
|
|
return (new HttpLogger($this->output))
|
|
|
|
|
->enable(config('app.debug'))
|
|
|
|
|
->verbose($this->output->isVerbose());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-24 14:23:59 +00:00
|
|
|
protected function configureMessageLogger()
|
2018-11-24 00:25:40 +00:00
|
|
|
{
|
2018-11-30 14:52:11 +00:00
|
|
|
app()->singleton(WebsocketsLogger::class, function() {
|
|
|
|
|
return (new WebsocketsLogger($this->output))
|
2018-11-24 14:23:59 +00:00
|
|
|
->enable(config('app.debug'))
|
2018-11-26 07:55:08 +00:00
|
|
|
->verbose($this->output->isVerbose());
|
2018-11-24 14:23:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function configureConnectionLogger()
|
|
|
|
|
{
|
|
|
|
|
app()->bind(ConnectionLogger::class, function() {
|
|
|
|
|
return (new ConnectionLogger($this->output))
|
2018-11-24 00:25:40 +00:00
|
|
|
->enable(config('app.debug'))
|
2018-11-26 07:55:08 +00:00
|
|
|
->verbose($this->output->isVerbose());
|
2018-11-24 00:25:40 +00:00
|
|
|
});
|
2018-11-22 07:49:26 +00:00
|
|
|
|
2018-11-24 00:25:40 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function registerEchoRoutes()
|
|
|
|
|
{
|
2018-11-30 14:51:30 +00:00
|
|
|
WebSocketsRouter::echo();
|
2018-11-22 07:49:26 +00:00
|
|
|
|
2018-11-24 00:25:40 +00:00
|
|
|
return $this;
|
2018-11-22 07:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-24 00:25:40 +00:00
|
|
|
protected function startWebSocketServer()
|
2018-11-22 07:49:26 +00:00
|
|
|
{
|
2018-11-24 00:35:08 +00:00
|
|
|
$this->info("Starting the WebSocket server on port {$this->option('port')}...");
|
2018-11-20 10:51:00 +00:00
|
|
|
|
2018-11-30 14:51:30 +00:00
|
|
|
$routes = WebSocketsRouter::getRoutes();
|
2018-11-20 10:51:00 +00:00
|
|
|
|
2018-11-30 14:42:40 +00:00
|
|
|
/** 🛰 Start the server 🛰 */
|
2018-11-27 20:53:50 +00:00
|
|
|
(new WebSocketServerFactory())
|
2018-12-03 12:33:20 +00:00
|
|
|
->setLoop($this->loop)
|
2018-11-27 20:53:50 +00:00
|
|
|
->useRoutes($routes)
|
2018-11-22 07:49:26 +00:00
|
|
|
->setHost($this->option('host'))
|
|
|
|
|
->setPort($this->option('port'))
|
2018-11-23 21:46:09 +00:00
|
|
|
->setConsoleOutput($this->output)
|
2018-11-27 20:53:50 +00:00
|
|
|
->createServer()
|
2018-11-24 00:25:40 +00:00
|
|
|
->run();
|
2018-11-20 10:51:00 +00:00
|
|
|
}
|
2018-11-24 00:25:40 +00:00
|
|
|
}
|