From ca4481fb93ab366b80ecd8da2b531b18069e92c4 Mon Sep 17 00:00:00 2001 From: freek Date: Thu, 22 Nov 2018 08:49:26 +0100 Subject: [PATCH] refactor websocket server --- src/Console/StartWebSocketServer.php | 42 +++++++++---------- src/Server/WebSocketServer.php | 60 ++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 31 deletions(-) diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index 3aef3ae..d5750c0 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -10,39 +10,35 @@ use React\EventLoop\Factory as LoopFactory; class StartWebSocketServer extends Command { + protected $signature = 'websocket:start {--host=0.0.0.0} {--port=6001} '; - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'websocket:start {--port=6001}'; - - /** - * The console command description. - * - * @var string - */ protected $description = 'Start the Laravel WebSocket Server'; - /** - * Execute the console command. - * - * @return void - */ public function handle() { + // 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(); + $loop = LoopFactory::create(); $loop->futureTick(function () { $this->info('Started the WebSocket server on port '.$this->option('port')); }); - // TODO: add an option to not start the echo server - WebSocketRouter::echo(); - - // TODO: add flag for verbose mode, to send more things to console - - (new WebsocketServer($this->option('port'), '0.0.0.0', $loop))->run(); + return (new WebSocketServer($routes)) + ->setHost($this->option('host')) + ->setPort($this->option('port')) + ->setLoop($loop); } } \ No newline at end of file diff --git a/src/Server/WebSocketServer.php b/src/Server/WebSocketServer.php index 6681469..39fc22b 100644 --- a/src/Server/WebSocketServer.php +++ b/src/Server/WebSocketServer.php @@ -5,28 +5,72 @@ namespace BeyondCode\LaravelWebSockets\Server; use Ratchet\Http\Router; use Ratchet\Http\HttpServer; use Ratchet\Server\IoServer; -use Ratchet\Server\FlashPolicy; use React\EventLoop\LoopInterface; use React\Socket\Server as Reactor; use React\EventLoop\Factory as LoopFactory; use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\Matcher\UrlMatcher; -use BeyondCode\LaravelWebSockets\Facades\WebSocketRouter; +use Symfony\Component\Routing\RouteCollection; class WebSocketServer { - /** @var \Ratchet\Server\IoServer */ - protected $server; + /** @var string */ + protected $host = '127.0.0.1'; - public function __construct($port = 8080, $address = '127.0.0.1', LoopInterface $loop) + /** @var int */ + protected $port = 8080; + + /** @var \React\EventLoop\LoopInterface */ + protected $loop; + + /** @var \Symfony\Component\Routing\RouteCollection */ + protected $routes; + + public function __construct(RouteCollection $routes) { - $socket = new Reactor($address . ':' . $port, $loop); + $this->loop = LoopFactory::create(); - $this->server = new IoServer(new HttpServer(new Router(new UrlMatcher(WebSocketRouter::getRoutes(), new RequestContext))), $socket, $loop); + $this->routes = $routes; + } + + public function setHost(string $host) + { + $this->host = $host; + + return $this; + } + + public function setPort(string $port) + { + $this->port = $port; + + return $this; + } + + public function setLoop(LoopInterface $loop) + { + $this->loop = $loop; + + return $this; } public function run() { - $this->server->run(); + $server = $this->createServer(); + + $server->run(); + } + + protected function createServer(): IoServer + { + $socket = new Reactor("{$this->host}:{$this->port}", $this->loop); + + $urlMatcher = new UrlMatcher($this->routes, new RequestContext); + + $router = new Router($urlMatcher); + + $httpServer = new HttpServer($router); + + return new IoServer($httpServer, $socket, $this->loop); } }