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-11-21 20:23:25 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Facades\WebSocketRouter;
|
2018-11-24 14:23:59 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\Server\Logger\MessageLogger;
|
2018-11-20 10:51:00 +00:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\Server\WebSocketServer;
|
|
|
|
|
|
|
|
|
|
use React\EventLoop\Factory as LoopFactory;
|
|
|
|
|
|
|
|
|
|
class StartWebSocketServer extends Command
|
|
|
|
|
{
|
2018-11-22 07:49:26 +00:00
|
|
|
protected $signature = 'websocket:start {--host=0.0.0.0} {--port=6001} ';
|
2018-11-20 10:51:00 +00:00
|
|
|
|
|
|
|
|
protected $description = 'Start the Laravel WebSocket Server';
|
|
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
2018-11-24 00:25:40 +00:00
|
|
|
$this
|
2018-11-24 14:23:59 +00:00
|
|
|
->configureMessageLogger()
|
|
|
|
|
->configureConnectionLogger()
|
2018-11-24 00:25:40 +00:00
|
|
|
->registerEchoRoutes()
|
|
|
|
|
->startWebSocketServer();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-24 14:23:59 +00:00
|
|
|
protected function configureMessageLogger()
|
2018-11-24 00:25:40 +00:00
|
|
|
{
|
2018-11-24 14:23:59 +00:00
|
|
|
app()->singleton(MessageLogger::class, function() {
|
|
|
|
|
return (new MessageLogger($this->output))
|
|
|
|
|
->enable(config('app.debug'))
|
|
|
|
|
//TODO: use real option
|
|
|
|
|
->verbose($this->hasOption('vvv'));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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'))
|
|
|
|
|
//TODO: use real option
|
|
|
|
|
->verbose($this->hasOption('vvv'));
|
|
|
|
|
});
|
2018-11-22 07:49:26 +00:00
|
|
|
|
2018-11-24 00:25:40 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function registerEchoRoutes()
|
|
|
|
|
{
|
|
|
|
|
WebSocketRouter::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-24 00:35:08 +00:00
|
|
|
$routes = WebSocketRouter::getRoutes();
|
2018-11-20 10:51:00 +00:00
|
|
|
|
2018-11-24 00:25:40 +00:00
|
|
|
/** 🎩 Start the magic 🎩 */
|
2018-11-24 14:23:59 +00:00
|
|
|
(new WebSocketServer($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-24 00:25:40 +00:00
|
|
|
->run();
|
2018-11-20 10:51:00 +00:00
|
|
|
}
|
2018-11-24 00:25:40 +00:00
|
|
|
}
|