laravel-websockets/src/Console/StartWebSocketServer.php

44 lines
1.2 KiB
PHP
Raw Normal View History

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-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-22 07:49:26 +00:00
// TODO: add an option to not start the echo server
WebSocketRouter::echo();
// TODO: add flag for verbose mode, to send more things to console
$websocketServer = $this->createWebsocketServer();
$websocketServer->run();
}
protected function createWebsocketServer(): WebSocketServer
{
$routes = WebSocketRouter::getRoutes();
2018-11-20 10:51:00 +00:00
$loop = LoopFactory::create();
2018-11-21 20:23:25 +00:00
$loop->futureTick(function () {
2018-11-20 10:51:00 +00:00
$this->info('Started the WebSocket server on port '.$this->option('port'));
});
2018-11-22 07:49:26 +00:00
return (new WebSocketServer($routes))
->setHost($this->option('host'))
->setPort($this->option('port'))
->setLoop($loop);
2018-11-20 10:51:00 +00:00
}
}