2018-11-27 20:30:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
namespace BeyondCode\LaravelWebSockets;
|
2018-11-27 20:30:33 +00:00
|
|
|
|
|
|
|
|
use Ratchet\Http\Router;
|
|
|
|
|
use Ratchet\Server\IoServer;
|
|
|
|
|
use React\EventLoop\Factory as LoopFactory;
|
2020-03-04 09:58:39 +00:00
|
|
|
use React\EventLoop\LoopInterface;
|
|
|
|
|
use React\Socket\SecureServer;
|
|
|
|
|
use React\Socket\Server;
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher;
|
2018-11-27 20:30:33 +00:00
|
|
|
use Symfony\Component\Routing\RequestContext;
|
|
|
|
|
use Symfony\Component\Routing\RouteCollection;
|
2020-09-10 19:59:26 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Server\HttpServer;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\Server\Loggers\HttpLogger;
|
2018-11-27 20:30:33 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
class ServerFactory
|
2018-11-27 20:30:33 +00:00
|
|
|
{
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* The host the server will run on.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2018-11-27 20:30:33 +00:00
|
|
|
protected $host = '127.0.0.1';
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* The port to run on.
|
|
|
|
|
*
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
2018-11-27 20:30:33 +00:00
|
|
|
protected $port = 8080;
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* The event loop instance.
|
|
|
|
|
*
|
|
|
|
|
* @var \React\EventLoop\LoopInterface
|
|
|
|
|
*/
|
2018-11-27 20:30:33 +00:00
|
|
|
protected $loop;
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* The routes to register.
|
|
|
|
|
*
|
|
|
|
|
* @var \Symfony\Component\Routing\RouteCollection
|
|
|
|
|
*/
|
2018-11-27 20:30:33 +00:00
|
|
|
protected $routes;
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Console output.
|
|
|
|
|
*
|
|
|
|
|
* @var Symfony\Component\Console\Output\OutputInterface
|
|
|
|
|
*/
|
2018-11-27 20:30:33 +00:00
|
|
|
protected $consoleOutput;
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Initialize the class.
|
|
|
|
|
*
|
|
|
|
|
* @param string $host
|
|
|
|
|
* @param int $port
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(string $host, int $port)
|
2018-11-27 20:30:33 +00:00
|
|
|
{
|
2020-08-18 17:21:22 +00:00
|
|
|
$this->host = $host;
|
|
|
|
|
$this->port = $port;
|
|
|
|
|
|
2018-11-27 20:30:33 +00:00
|
|
|
$this->loop = LoopFactory::create();
|
2018-11-27 20:53:50 +00:00
|
|
|
}
|
2018-11-27 20:30:33 +00:00
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Add the routes.
|
|
|
|
|
*
|
2020-09-10 19:59:26 +00:00
|
|
|
* @param \Symfony\Component\Routing\RouteCollection $routes
|
2020-08-18 17:21:22 +00:00
|
|
|
* @return $this
|
|
|
|
|
*/
|
2020-09-10 19:59:26 +00:00
|
|
|
public function withRoutes(RouteCollection $routes)
|
2018-11-27 20:53:50 +00:00
|
|
|
{
|
2018-11-27 20:30:33 +00:00
|
|
|
$this->routes = $routes;
|
2018-11-27 20:53:50 +00:00
|
|
|
|
|
|
|
|
return $this;
|
2018-11-27 20:30:33 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Set the loop instance.
|
|
|
|
|
*
|
|
|
|
|
* @param \React\EventLoop\LoopInterface $loop
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2018-11-27 20:30:33 +00:00
|
|
|
public function setLoop(LoopInterface $loop)
|
|
|
|
|
{
|
|
|
|
|
$this->loop = $loop;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Set the console output.
|
|
|
|
|
*
|
|
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $consoleOutput
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2018-11-27 20:30:33 +00:00
|
|
|
public function setConsoleOutput(OutputInterface $consoleOutput)
|
|
|
|
|
{
|
|
|
|
|
$this->consoleOutput = $consoleOutput;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Set up the server.
|
|
|
|
|
*
|
|
|
|
|
* @return \Ratchet\Server\IoServer
|
|
|
|
|
*/
|
2018-11-27 20:53:50 +00:00
|
|
|
public function createServer(): IoServer
|
2018-11-27 20:30:33 +00:00
|
|
|
{
|
|
|
|
|
$socket = new Server("{$this->host}:{$this->port}", $this->loop);
|
|
|
|
|
|
|
|
|
|
if (config('websockets.ssl.local_cert')) {
|
|
|
|
|
$socket = new SecureServer($socket, $this->loop, config('websockets.ssl'));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 13:04:52 +00:00
|
|
|
$app = new Router(
|
|
|
|
|
new UrlMatcher($this->routes, new RequestContext)
|
|
|
|
|
);
|
2018-11-27 20:30:33 +00:00
|
|
|
|
2018-12-01 14:53:41 +00:00
|
|
|
$httpServer = new HttpServer($app, config('websockets.max_request_size_in_kb') * 1024);
|
2018-11-27 20:30:33 +00:00
|
|
|
|
|
|
|
|
if (HttpLogger::isEnabled()) {
|
|
|
|
|
$httpServer = HttpLogger::decorate($httpServer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new IoServer($httpServer, $socket, $this->loop);
|
|
|
|
|
}
|
|
|
|
|
}
|