/** * {@inheritdoc} */ public function start(ServerContextInterface $context, RequestHandlerInterface $requestHandler) { $this->loop = Factory::create(); $this->socketServer = new SocketServer($this->loop); $this->httpServer = new HttpServer($this->socketServer); $this->httpServer->on('request', $this->createRequestHandler($requestHandler)); $this->socketServer->listen($context->getListenPort(), $context->getListenAddress()); $this->loop->run(); }
/** * @param array $options * @param array $arguments * @return mixed */ public function run(array $options, array $arguments) { $port = isset($arguments[0]) ? $arguments[0] : "8001"; $host = isset($arguments[1]) ? $arguments[1] : "0.0.0.0"; /** @var \Castle23\Http\Server $server */ $server = $this->container->getInstanceOf(Server::class); $this->socket->listen($port, $host); $this->loop->run(); }
/** * Runs the message server on the given port. * * @param string $consumer Command to execute when a message arrives * @param integer $port Port to run the server on * * @return void */ public function run($consumer, $port, $callback = null) { // @codeCoverageIgnoreStart $this->socket->on('connection', function (ConnectionInterface $conn) use($consumer, $callback) { $conn->on('data', function ($data) use($conn, $consumer, $callback) { $this->handleData(trim($data), $consumer, $conn, $callback); }); }); // @codeCoverageIgnoreEnd $this->socket->listen($port); $this->loop->run(); }
/** * Run react. * * @param int $port * @param string $host */ public function run($port, $host) { $request_handler = function (Request $request, Response $response) { echo $request->getMethod() . ' ' . $request->getPath() . PHP_EOL; $sf_request = $this->request_bridge->convertRequest($request); $sf_response = $this->app->handle($sf_request); $this->app->terminate($sf_request, $sf_response); $this->response_bridge->send($response, $sf_response); }; $this->http->on('request', $request_handler); $this->socket->listen($port, $host); $this->loop->run(); }
/** * Find a free port for the server and start it. * @param int $minPort Min port in range (inclusive). * @param int $maxPort Max port in range (inclusive). * @param int $port Actual selected port (output). */ protected function startServer($minPort, $maxPort, &$port) { if ($minPort > $maxPort) { throw new InitException("Invalid port range min ({$minPort}) is bigger than max ({$maxPort})."); } $retries = 0; $connected = false; while (!$connected && $retries < self::SERVER_START_RETRIES) { $port = rand($minPort, $maxPort); try { $this->server->listen($port); $connected = true; } catch (ConnectionException $e) { $retries++; if ($retries >= self::SERVER_START_RETRIES) { throw new InitException("Failed to start server " . $e->getMessage(), $e); } } } }