laravel-websockets/src/Server/WebSocketServerFactory.php

99 lines
2.3 KiB
PHP
Raw Normal View History

2018-11-27 20:30:33 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\Server;
use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger;
use Ratchet\Http\Router;
use React\Socket\SecureServer;
use React\Socket\Server;
use Ratchet\Server\IoServer;
use React\EventLoop\LoopInterface;
use React\EventLoop\Factory as LoopFactory;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RouteCollection;
2018-11-27 20:46:03 +00:00
class WebSocketServerFactory
2018-11-27 20:30:33 +00:00
{
/** @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;
/** @var Symfony\Component\Console\Output\OutputInterface */
protected $consoleOutput;
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 setConsoleOutput(OutputInterface $consoleOutput)
{
$this->consoleOutput = $consoleOutput;
return $this;
}
public function run()
{
$server = $this->createServer();
$server->run();
}
protected function createServer(): IoServer
{
$socket = new Server("{$this->host}:{$this->port}", $this->loop);
if (config('websockets.ssl.local_cert')) {
$socket = new SecureServer($socket, $this->loop, config('websockets.ssl'));
}
$urlMatcher = new UrlMatcher($this->routes, new RequestContext);
$router = new Router($urlMatcher);
$app = new OriginCheck($router, config('websockets.allowedOrigins', []));
2018-11-27 20:32:49 +00:00
$httpServer = new HttpServer($app, config('websockets.maxRequestSizeInKb') * 1024);
2018-11-27 20:30:33 +00:00
if (HttpLogger::isEnabled()) {
$httpServer = HttpLogger::decorate($httpServer);
}
return new IoServer($httpServer, $socket, $this->loop);
}
}