loop = LoopFactory::create(); } public function setHost(string $host) { $this->host = $host; return $this; } public function setPort(int $port) { $this->port = $port; return $this; } public function setLoop(LoopInterface $loop) { $this->loop = $loop; return $this; } public function setHostname(string $hostname) { $this->hostname = $hostname; return $this; } protected function getRoutes(): RouteCollection { $routes = new RouteCollection(); $routes->add('control', new Route('/__expose_control__', [ '_controller' => new WsServer(app(ControlMessageController::class)) ], [], [], null, [], [] ) ); $routes->add('tunnel', new Route('/{__catchall__}', [ '_controller' => app(TunnelMessageController::class), ], [ '__catchall__' => '.*' ])); return $routes; } protected function bindConfiguration() { app()->singleton(Configuration::class, function ($app) { return new Configuration($this->hostname, $this->port); }); } protected function bindSubdomainGenerator() { app()->singleton(SubdomainGenerator::class, function ($app) { return $app->make(RandomSubdomainGenerator::class); }); } protected function bindConnectionManager() { app()->singleton(ConnectionManagerContract::class, function ($app) { return $app->make(ConnectionManager::class); }); } public function createServer() { $socket = new Server("{$this->host}:{$this->port}", $this->loop); $this->bindConfiguration(); $this->bindSubdomainGenerator(); $this->bindConnectionManager(); $urlMatcher = new UrlMatcher($this->getRoutes(), new RequestContext); $router = new Router($urlMatcher); $http = new HttpServer($router); return new IoServer($http, $socket, $this->loop); } }