laravel-websockets/src/Server/WebSocketServer.php

77 lines
1.6 KiB
PHP

<?php
namespace BeyondCode\LaravelWebSockets\Server;
use Ratchet\Http\Router;
use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;
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 Symfony\Component\Routing\RouteCollection;
class WebSocketServer
{
/** @var string */
protected $host = '127.0.0.1';
/** @var int */
protected $port = 8080;
/** @var \React\EventLoop\LoopInterface */
protected $loop;
/** @var \Symfony\Component\Routing\RouteCollection */
protected $routes;
public function __construct(RouteCollection $routes)
{
$this->loop = LoopFactory::create();
$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()
{
$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);
}
}