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
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue