refactor websocket server
This commit is contained in:
parent
10ad9cd167
commit
ca4481fb93
|
|
@ -10,39 +10,35 @@ use React\EventLoop\Factory as LoopFactory;
|
||||||
|
|
||||||
class StartWebSocketServer extends Command
|
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';
|
protected $description = 'Start the Laravel WebSocket Server';
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute the console command.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function handle()
|
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 = LoopFactory::create();
|
||||||
|
|
||||||
$loop->futureTick(function () {
|
$loop->futureTick(function () {
|
||||||
$this->info('Started the WebSocket server on port '.$this->option('port'));
|
$this->info('Started the WebSocket server on port '.$this->option('port'));
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: add an option to not start the echo server
|
return (new WebSocketServer($routes))
|
||||||
WebSocketRouter::echo();
|
->setHost($this->option('host'))
|
||||||
|
->setPort($this->option('port'))
|
||||||
// TODO: add flag for verbose mode, to send more things to console
|
->setLoop($loop);
|
||||||
|
|
||||||
(new WebsocketServer($this->option('port'), '0.0.0.0', $loop))->run();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -5,28 +5,72 @@ namespace BeyondCode\LaravelWebSockets\Server;
|
||||||
use Ratchet\Http\Router;
|
use Ratchet\Http\Router;
|
||||||
use Ratchet\Http\HttpServer;
|
use Ratchet\Http\HttpServer;
|
||||||
use Ratchet\Server\IoServer;
|
use Ratchet\Server\IoServer;
|
||||||
use Ratchet\Server\FlashPolicy;
|
|
||||||
use React\EventLoop\LoopInterface;
|
use React\EventLoop\LoopInterface;
|
||||||
use React\Socket\Server as Reactor;
|
use React\Socket\Server as Reactor;
|
||||||
use React\EventLoop\Factory as LoopFactory;
|
use React\EventLoop\Factory as LoopFactory;
|
||||||
use Symfony\Component\Routing\RequestContext;
|
use Symfony\Component\Routing\RequestContext;
|
||||||
use Symfony\Component\Routing\Matcher\UrlMatcher;
|
use Symfony\Component\Routing\Matcher\UrlMatcher;
|
||||||
use BeyondCode\LaravelWebSockets\Facades\WebSocketRouter;
|
use Symfony\Component\Routing\RouteCollection;
|
||||||
|
|
||||||
class WebSocketServer
|
class WebSocketServer
|
||||||
{
|
{
|
||||||
/** @var \Ratchet\Server\IoServer */
|
/** @var string */
|
||||||
protected $server;
|
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()
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue