/** * Start the HTTP Server */ public function start() { $this->httpServer->on('request', [$this, 'handleRequest']); $this->logger->info("Server is listening at " . $this->configuration['listen']['host'] . ":" . $this->configuration['listen']['port']); $this->socketServer->listen($this->configuration['listen']['port'], $this->configuration['listen']['host']); $this->eventLoop->run(); }
/** * @param string $httpHost HTTP hostname clients intend to connect to. MUST match JS `new WebSocket('ws://$httpHost');` * @param int $port Port to listen on. If 80, assuming production, Flash on 843 otherwise expecting Flash to be proxied through 8843 * @param string $address IP address to bind to. Default is localhost/proxy only. '0.0.0.0' for any machine. * @param LoopInterface $loop Specific React\EventLoop to bind the application to. null will create one for you. */ public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1', LoopInterface $loop = null, $context = array()) { if (extension_loaded('xdebug')) { trigger_error("XDebug extension detected. Remember to disable this if performance testing or going live!", E_USER_WARNING); } if (3 !== strlen('✓')) { throw new \DomainException('Bad encoding, length of unicode character ✓ should be 3. Ensure charset UTF-8 and check ini val mbstring.func_autoload'); } if (null === $loop) { $loop = LoopFactory::create(); } $this->httpHost = $httpHost; $socket = new Reactor($loop, $context); $socket->listen($port, $address); $this->routes = new RouteCollection(); $this->_server = new IoServer(new HttpServer(new Router(new UrlMatcher($this->routes, new RequestContext()))), $socket, $loop); $policy = new FlashPolicy(); if ('*' !== $httpHost) { $policy->addAllowedAccess($httpHost, 80); $policy->addAllowedAccess($httpHost, $port); } $flashSock = new Reactor($loop); $this->flashServer = new IoServer($policy, $flashSock); if (80 == $port) { $flashSock->listen(843, '0.0.0.0'); } else { $flashSock->listen(8843); } }
public function __construct($realmName, $loop = null, $bindAddress = '127.0.0.1', $port = 8181) { if ($loop === null) { $loop = Factory::create(); } $this->bindAddress = $bindAddress; $this->port = $port; $this->realmName = $realmName; $this->socket = new Server($loop); $this->http = new \React\Http\Server($this->socket); $this->http->on('request', [$this, 'handleRequest']); $this->socket->listen($this->port, $bindAddress); parent::__construct($realmName, $loop); }
/** * Sets up loop and server manually to allow periodic timer calls. */ private function setupServer() { $this->setupApp(); /** @var $loop \React\EventLoop\LoopInterface */ $this->loop = \React\EventLoop\Factory::create(); $this->socket = new Server($this->loop); if ($this->host) { $this->socket->listen($this->port, $this->host); } else { $this->socket->listen($this->port); } $this->setupPeriodicServices(); $this->setupSocketListeners(); $this->server = new \Ratchet\Server\IoServer($this->app, $this->socket, $this->loop); }
/** * @param int $port * @param string $host */ public function listen($port, $host) { $this->http->on('request', function (ReactRequest $request, ReactResponse $response) { return $this->onRequest($request, $response); }); $this->logger->info("Phiremock http server listening on {$host}:{$port}"); $this->socket->listen($port, $host); // Dispatch pending signals periodically if (function_exists('pcntl_signal_dispatch')) { $this->loop->addPeriodicTimer(0.5, function () { pcntl_signal_dispatch(); }); } $this->loop->run(); }
public function start() { $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $server = $this; $socket->on(self::CONNECTION, function (React\Socket\Connection $connection) use(&$server) { $server->connectionEvent($connection); $connection->on(SyncDaemonServer::DATA, function ($data) use(&$connection, &$server) { list($command, $name) = explode(' ', $data); if ($command == SyncDaemonServer::ACQUIRE) { $server->acquireCommand($connection, $name); } if ($command == SyncDaemonServer::RELEASE) { $server->releaseCommand($connection, $name); } }); $connection->on(SyncDaemonServer::CLOSE, function () use(&$connection, &$server) { $server->closeEvent($connection); }); $connection->write(SyncDaemonServer::ACCEPT); }); //debug //$loop->addPeriodicTimer(1, function () use (&$server) { // var_dump(count($server->connections) . '-' . count($server->locks)); //}); $socket->listen($this->port, $this->host); $loop->run(); }
public function __construct(QueueContextInterface $queueContext, LoopInterface $eventLoop, $port) { $this->queueContext = $queueContext; $server = new Server($eventLoop); $server->on('connection', [$this, 'onConnection']); $server->listen($port); }
public function addListener($host = '127.0.0.1', $port = 8080, $use_ssl = false, $cert = null) { $proxy = new Server($this->loop); $proxy->on('connection', new ConnectionHandler($this)); $context = !$use_ssl ? [] : ['ssl' => ['local_cert' => $cert === null ? __DIR__ . '/../certificate.pem' : $cert, 'allow_self_signed' => true, 'verify_peer' => false]]; $proxy->listen($port, $host, $context); }
/** * Creates a new \React\Http\Server and runs it * * @param int $port */ public function listen($port) { $socket = new SocketServer($this->loop); $server = new HttpServer($socket); $server->on('request', $this->requestHandler); $socket->listen($port); }
public function assertConnection(array $options, $message = null) { $settings = array_merge(["ip" => "0.0.0.0", "port" => 0, "startServer" => false, "match" => true], $options); // optionally starting server if ($settings["startServer"]) { $serverLoop = EventLoopFactory::create(); $server = new SocketServer($serverLoop); $server->listen($settings["port"]); } // client setup $clientLoop = EventLoopFactory::create(); $dnsResolverFactory = new DnsResolverFactory(); $dns = $dnsResolverFactory->createCached("8.8.8.8", $clientLoop); // dunno why dns is required for this shit $connector = new SocketConnector($clientLoop, $dns); $promise = $connector->create($settings["ip"], $settings["port"])->then(function (SocketStream $stream) { $stream->close(); return true; }, function (SocketConnectionException $e) { return false; }); $clientLoop->run(); // catching the output $out = null; $promise->done(function ($v) use(&$out) { $out = $v; }); // optionally cleaning up the server if ($settings["startServer"]) { $server->shutdown(); } $this->assertEquals($out, $settings["match"], $message); }
/** * Execute the console command. * * @return mixed */ public function fire() { // create a log channel $log = new Logger('websocket'); $log->pushHandler(new RotatingFileHandler(storage_path() . '/logs/websocket.log', 10, Logger::DEBUG)); $config = Config::get('announcements-server'); $loop = LoopFactory::create(); $announcements = new AnnouncementsWebSocket($log); // Listen for the web server to make a message push. $broadcast = 'tcp://' . $config['broadcast']['ip'] . ':' . $config['broadcast']['port']; $this->info('Starting broadcast socket on ' . $broadcast); $context = new ZMQContext($loop); $broadcastSocket = $context->getSocket(ZMQ::SOCKET_PULL); $broadcastSocket->bind($broadcast); $broadcastSocket->on('message', array($announcements, 'onBroadcast')); // Listen for status check. $status = 'tcp://' . $config['status']['ip'] . ':' . $config['status']['port']; $this->info('Starting status socket on ' . $status); $statusSock = new SocketServer($loop); $statusSock->listen($config['status']['port'], $config['status']['ip']); new IoServer(new AnnouncementsServerStatus(), $statusSock); // Listen for WebSocket connections. $wsPort = $config['websocket']['port']; $wsIp = $config['websocket']['ip']; $this->info('Starting WebSocket socket on ws://' . $wsIp . ':' . $wsPort); $webSock = new SocketServer($loop); $webSock->listen($wsPort, $wsIp); new IoServer(new HttpServer(new WsServer($announcements)), $webSock); // Ping all clients each 2 min. $loop->addPeriodicTimer(120, function () use($announcements) { $announcements->ping(); }); $log->info('Server started'); $loop->run(); }
/** * Set up our WebSocket server for clients wanting real-time updates * * @param StreamSelectLoop $loop * @return Ratchet\Server\IoServer */ protected function setUpWebSocket(StreamSelectLoop $loop) { $webSock = new Server($loop); // Binding to 0.0.0.0 means remotes can connect $webSock->listen(static::SERVER_PORT, '0.0.0.0'); return new IoServer(new HttpServer(new WsServer(new WampServer($this->pusher))), $webSock); }
/** * Starts the main loop. Blocks. */ public function run() { Debug::enable(); //make whatever is necessary to disable all stuff that could buffer output ini_set('zlib.output_compression', 0); ini_set('output_buffering', 0); ini_set('implicit_flush', 1); ob_implicit_flush(1); $this->loop = \React\EventLoop\Factory::create(); $this->controller = new React\Server($this->loop); $this->controller->on('connection', array($this, 'onSlaveConnection')); $this->controllerHost = $this->getNewControllerHost(); $this->controller->listen(5500, $this->controllerHost); $this->web = new \React\Socket\Server($this->loop); $this->web->on('connection', array($this, 'onWeb')); $this->web->listen($this->port, $this->host); $this->tcpConnector = new \React\SocketClient\TcpConnector($this->loop); $pcntl = new \MKraemer\ReactPCNTL\PCNTL($this->loop); $pcntl->on(SIGTERM, [$this, 'shutdown']); $pcntl->on(SIGINT, [$this, 'shutdown']); $pcntl->on(SIGCHLD, [$this, 'handleSigchld']); $pcntl->on(SIGUSR1, [$this, 'restartWorker']); if ($this->isDebug()) { $this->loop->addPeriodicTimer(0.5, function () { $this->checkChangedFiles(); }); } $this->isRunning = true; $loopClass = (new \ReflectionClass($this->loop))->getShortName(); $this->output->writeln("<info>Starting PHP-PM with {$this->slaveCount} workers, using {$loopClass} ...</info>"); for ($i = 0; $i < $this->slaveCount; $i++) { $this->newInstance(5501 + $i); } $this->loop->run(); }
protected function execute(InputInterface $input, OutputInterface $output) { // Make sure that websockets are enabled in the config so that the proper // session storage handler is used if (!$this->getContainer()->getParameter('bzion.features.websocket.enabled')) { $message = "You need to enable websockets in your config before using the push server"; $output->writeln("<bg=red;options=bold>\n\n [ERROR] {$message}\n</>"); return; } $loop = EventLoopFactory::create(); $pusher = new EventPusher($loop, $output); $pushPort = $input->getOption('push') ?: $this->getContainer()->getParameter('bzion.features.websocket.push_port'); $pullPort = $input->getOption('pull') ?: $this->getContainer()->getParameter('bzion.features.websocket.pull_port'); $pullSocket = new Server($loop); $pullSocket->on('connection', function ($conn) use($pusher) { $conn->on('data', function ($data) use($pusher) { $pusher->onServerEvent(json_decode($data)); }); }); // Bind to 127.0.0.1, so that only the server can send messages to the socket $pullSocket->listen($pullPort, '127.0.0.1'); $output->writeln(" <fg=green>Running pull service on port {$pullPort}</>"); $session = new SessionProvider($pusher, new DatabaseSessionHandler()); $pushSocket = new Server($loop); $webServer = new IoServer(new WsServer($session), $pushSocket); // Binding to 0.0.0.0 means remotes can connect $pushSocket->listen($pushPort, '0.0.0.0'); $output->writeln(" <fg=green>Running push service on port {$pushPort}</>"); $output->writeln("\n <bg=green;options=bold>Welcome to the BZiON live notification server!</>"); $loop->run(); }
/** * Constructor * * @param OutputInterface $output Output * @param string $docroot Docroot * @param string $env Environment * @param bool $debug Debug * @param int $port Port */ public function __construct(OutputInterface $output, $docroot, $env, $debug, $port = null) { $repository = new PhpRepository(); if (!$port) { $port = 8000; } $this->output = $output; $this->env = $env; $this->debug = $debug; $this->port = $port; $this->loop = new StreamSelectLoop(); $socketServer = new ReactSocketServer($this->loop); $httpServer = new ReactHttpServer($socketServer); $httpServer->on("request", function ($request, $response) use($repository, $docroot, $output) { $path = $docroot . '/' . ltrim(rawurldecode($request->getPath()), '/'); if (is_dir($path)) { $path .= '/index.html'; } if (!file_exists($path)) { HttpServer::logRequest($output, 404, $request); $response->writeHead(404, ['Content-Type' => 'text/html']); return $response->end(implode('', ['<h1>404</h1>', '<h2>Not Found</h2>', '<p>', 'The embedded <a href="https://sculpin.io">Sculpin</a> web server could not find the requested resource.', '</p>'])); } $type = 'application/octet-stream'; if ('' !== ($extension = pathinfo($path, PATHINFO_EXTENSION))) { if ($guessedType = $repository->findType($extension)) { $type = $guessedType; } } HttpServer::logRequest($output, 200, $request); $response->writeHead(200, array("Content-Type" => $type)); $response->end(file_get_contents($path)); }); $socketServer->listen($port, '0.0.0.0'); }
/** * Starts the main loop. Blocks. */ public function run() { Debug::enable(); gc_disable(); //necessary, since connections will be dropped without reasons after several hundred connections. $this->loop = \React\EventLoop\Factory::create(); $this->controller = new \React\Socket\Server($this->loop); $this->controller->on('connection', array($this, 'onSlaveConnection')); $this->controller->listen(5500); $this->web = new \React\Socket\Server($this->loop); $this->web->on('connection', array($this, 'onWeb')); $this->web->listen($this->port, $this->host); $this->tcpConnector = new \React\SocketClient\TcpConnector($this->loop); $pcntl = new \MKraemer\ReactPCNTL\PCNTL($this->loop); $pcntl->on(SIGTERM, [$this, 'shutdown']); $pcntl->on(SIGINT, [$this, 'shutdown']); if ($this->isDebug()) { $this->loop->addPeriodicTimer(0.5, function () { $this->checkChangedFiles(); }); } $this->isRunning = true; $loopClass = (new \ReflectionClass($this->loop))->getShortName(); $this->output->writeln("<info>Starting PHP-PM with {$this->slaveCount} workers, using {$loopClass} ...</info>"); for ($i = 0; $i < $this->slaveCount; $i++) { $this->newInstance(5501 + $i); } $this->loop->run(); }
private function initHttpServer() { $socket = new ServerSocket($this->getLoop()); $socket->listen($this->config->getPort(), $this->config->getHost()); $http = new HttpServer($socket, $this->getLoop()); $http->on('request', [$this, 'handleRequest']); }
public static function factory(MessageComponentInterface $component, $port = 80, $address = '0.0.0.0') { $loop = new StreamSelectLoop(); $socket = new Reactor($loop); $socket->listen($port, $address); return new static($component, $socket, $loop); }
/** * Setup the web socket server. * * @param $loop * @throws \React\Socket\ConnectionException */ protected function setupWebSocketServer($loop) { $webSock = new Server($loop); $webSock->listen(Config::get('socket.socketPort', 8080), '0.0.0.0'); $webServer = new IoServer(new HttpServer(new WsServer(new WampServer($this->socketServer))), $webSock); $this->info(sprintf('Server listening at 0.0.0.0:%s', Config::get('socket.socketPort', 8080))); }
/** * {@inheritDoc} * @see \Symfony\Component\Console\Command\Command::execute() */ protected function execute(InputInterface $input, OutputInterface $output) { // Start listenning $this->socket->listen($this->port, $this->host); // Periodically call determining if we should stop or not $this->loop->addPeriodicTimer($input->getOption('check-interval'), function () use($output) { if ($this->shouldExitCommand($output)) { $this->loop->stop(); $this->writeln($output, 'Event loop stopped:' . $this->port); $this->returnValue = 10; } }); // Main loop $this->writeln($output, 'Starting event loop:' . $this->port); $this->loop->run(); return $this->returnValue; }
/** * Start transport provider * * @param \Thruway\Peer\AbstractPeer $peer * @param \React\EventLoop\LoopInterface $loop */ public function startTransportProvider(AbstractPeer $peer, LoopInterface $loop) { $this->peer = $peer; $this->loop = $loop; $socket = new Server($loop); $socket->on('connection', [$this, "handleConnection"]); $socket->listen($this->port, $this->address); }
protected function createServerLoop($port) { $this->loop = Factory::create(); // Set up our WebSocket server for clients wanting real-time updates $webSock = new Server($this->loop); $webSock->listen($port, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect $webServer = new IoServer(new HttpServer(new WsServer(new WampServer($this->pusher))), $webSock); }
/** * @param string $httpHost HTTP hostname clients intend to connect to. MUST match JS `new WebSocket('ws://$httpHost');` * @param int $port Port to listen on. * @param string $address IP address to bind to. Default is localhost/proxy only. '0.0.0.0' for any machine. */ public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1') { $loop = LoopFactory::create(); $this->httpHost = $httpHost; $socket = new Reactor($loop); $socket->listen($port, $address); $this->routes = new RouteCollection(); $this->server = new IoServer(new HttpServer(new RachetRouter(new UrlMatcher($this->routes, new RequestContext()))), $socket, $loop); }
/** * WebSocketManager constructor. * * @param Configuration $configuration * @param LoggerInterface $logger * @param EventDispatcher $eventDispatcher * @param LoopInterface $loop */ public function __construct(Configuration $configuration, LoggerInterface $logger, EventDispatcher $eventDispatcher, LoopInterface $loop) { parent::__construct($configuration, $logger, $eventDispatcher); // Create the socket to listen on $socket = new ServerSocket($loop); $socket->listen($configuration->getListenPort(), $configuration->getListenAddress()); // Create the WebSocket server $this->_websocket = new IoServer(new HttpServer(new WsServer($this)), $socket, $loop); }
public function openConnection() { $this->client->open(); if (false !== ($this->webhooksConfig = $this->getWebhooksSettings())) { $this->startWebserver($this->webhooksConfig); $this->socket->listen($this->socketPort, $this->socketHost); } $this->loop->run(); }
/** * start the loop */ public function start() { $this->loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($this->loop); $http = new React\Http\Server($socket); $http->on('request', $function = $this->requestHandler->getHandlerFunction()); $socket->listen($this->requestHandler->getPort(), $this->requestHandler->getHost()); $this->loop->run(); }
/** * Starts the server */ public function start() { $loop = Factory::create(); $socketServer = new SocketServer($loop); $httpServer = new HttpServer($socketServer); $httpServer->on('request', array($this, 'serverCallback')); $socketServer->listen($this->port, $this->host); fwrite(STDOUT, 'Starting server at ' . $this->host . ':' . $this->port); $loop->run(); }
/** * Start transportprovider * * @param \Thruway\Peer\AbstractPeer $peer * @param \React\EventLoop\LoopInterface $loop */ public function startTransportProvider(AbstractPeer $peer, LoopInterface $loop) { $this->peer = $peer; $this->loop = $loop; $ws = new WsServer($this); $ws->disableVersion(0); $socket = new Reactor($this->loop); $socket->listen($this->port, $this->address); $this->server = new IoServer(new HttpServer($ws), $socket, $this->loop); }
public function listen($port, $host = '127.0.0.1') { $loop = Factory::create(); $socket = new SocketServer($loop); $http = new HttpServer($socket); $http->on('request', $this->app); echo "Server running on {$host}:{$port}\n"; $socket->listen($port, $host); $loop->run(); }
/** * @return void */ public function loop() { $loop = Factory::create(); $socket = new SocketServer($loop); $http = new HttpServer($socket); $this->getEventManager()->attach(MvcEvent::EVENT_FINISH, [$this, 'renderRequest'], -1000); $http->on('request', [$this, 'processRequest']); $socket->listen($this->serverOptions->getPort(), $this->serverOptions->getHost()); $loop->run(); }