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 15:29:47 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Logger\StatisticsLogger as StatisticsLoggerInterface;
|
|
|
|
|
|
2018-12-03 14:13:29 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
2018-12-03 20:06:49 +00:00
|
|
|
use Clue\React\Buzz\Browser;
|
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;
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
2018-12-03 20:06:49 +00:00
|
|
|
$connector = new \React\Socket\Connector($this->loop, array(
|
|
|
|
|
'dns' => '127.0.0.1'
|
|
|
|
|
));
|
2018-12-03 13:46:51 +00:00
|
|
|
|
2018-12-03 20:06:49 +00:00
|
|
|
$browser = new Browser($this->loop, $connector);
|
2018-12-03 13:46:51 +00:00
|
|
|
|
2018-12-03 20:06:49 +00:00
|
|
|
|
|
|
|
|
app()->singleton(StatisticsLoggerInterface::class, function() use ($browser) {
|
|
|
|
|
return new HttpStatisticsLogger(app(ChannelManager::class), $browser);
|
2018-12-03 13:46:51 +00:00
|
|
|
});
|
|
|
|
|
|
2018-12-03 15:50:06 +00:00
|
|
|
$this->loop->addPeriodicTimer(5, function() {
|
|
|
|
|
echo 'saving stats...';
|
2018-12-03 20:02:21 +00:00
|
|
|
StatisticsLogger::save();
|
2018-12-03 13:46:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|