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 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(); }
private function setupSocketListeners() { $connectionListeners = array(); foreach ($this->socketListeners as $listener) { /** @var SocketListenerInterface $service */ $service = $this->getContainer()->get($listener); if (!$service instanceof SocketListenerInterface) { throw new \Exception("Socket Listener Services must implement JDare/ClankBundle/Server/SocketListenerInterface"); } foreach ((array) $service->getListeners() as $event => $callback) { if ($event == 'connection') { $this->socket->on('connection', $callback); } elseif ($event == 'error') { $this->socket->on('error', $callback); } else { if (!isset($connectionListeners[$event])) { $connectionListeners[$event] = array(); } $connectionListeners[$event][] = $callback; } } } if (!empty($connectionListeners)) { $this->socket->on('connection', function (ConnectionInterface $conn) use(&$connectionListeners) { $conn->on('data', function ($data) use($conn, &$connectionListeners) { if (isset($connectionListeners[$data])) { foreach ($connectionListeners[$data] as $callback) { $callback($conn); } } }); }); } }
/** * 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(); }
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); }
/** * 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(); }
/** * @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 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); }
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'); }
/** * 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); }
/** * 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))); }
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 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); }
/** * 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); }
/** * 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); }
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); }
/** * 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); }
/** * @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(); }
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(); }
/** * @inheritdoc */ public function __construct(Configuration $configuration, LoggerInterface $logger, EventDispatcher $eventDispatcher, LoopInterface $loop) { parent::__construct($configuration, $logger, $eventDispatcher); $socket = new SocketServer($loop); $socket->listen($configuration->getHttpListenPort(), $configuration->getListenAddress()); // Configure the web server $webroot = realpath(__DIR__ . '/../../client/app'); $this->_staticWebServer = new StaticWebServer(new HttpServer($socket), $webroot, $logger); // Configure the authentication handler $this->_staticWebServer->setAuthenticationHandler(new BasicAuthenticationHandler('tvheadend-status-manager', [$this, 'validateCredentials'])); }
/** * Starts the websocket server * * @return void */ public function start() { $this->__loop = LoopFactory::create(); if ($this->__loop instanceof \React\EventLoop\StreamSelectLoop) { $this->out('<warning>Your configuration doesn\'t seem to support \'ext-libevent\'. It is highly reccomended that you install and configure it as it provides significant performance gains over stream select!</warning>'); } $socket = new Reactor($this->__loop); $socket->listen(Configure::read('Ratchet.Connection.websocket.port'), Configure::read('Ratchet.Connection.websocket.address')); $this->__ioServer = new IoServer(new HttpServer(new WsServer(new SessionProvider(new WampServer(new CakeWampAppServer($this, $this->__loop, CakeEventManager::instance(), $this->params['verbose'])), new CakeWampSessionHandler(), [], new PhpSerializeHandler()))), $socket, $this->__loop); $this->__loop->run(); }
public function run($port) { self::$port = $port; $loop = new StreamSelectLoop(); $socket = new SocketServer($loop); $http = new HttpServer($socket, $loop); $http->on('request', [$this, 'serve']); $socket->listen($port); echo "Reactavel server started on localhost:{$port}\n"; $loop->run(); }
protected function execute(InputInterface $input, OutputInterface $output) { $this->input = $input; $this->output = $output; $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $http = new React\Http\Server($socket); $http->on('request', function (React\Http\Request $request, React\Http\Response $response) { $this->handleRequest($request, $response); }); $socket->listen(8000); $loop->run(); }
/** * Start the websocket server */ public function start() { $this->__loop = Factory::create(); $pusher = new CakeWampAppServer(); $context = new Context($this->__loop); $pull = $context->getSocket(ZMQ::SOCKET_PULL); $pull->bind("tcp://" . Configure::read('CakeRatchet.ZMQServer.host') . ":" . Configure::read('CakeRatchet.ZMQServer.port')); $pull->on('message', array($pusher, 'onBlogEntry')); $webSock = new Server($this->__loop); $webSock->listen(Configure::read('CakeRatchet.Server.port'), Configure::read('CakeRatchet.Server.host')); $this->__ioServer = new IoServer(new HttpServer(new WsServer(new WampServer($pusher))), $webSock); $this->__loop->run(); }
protected function execute(InputInterface $input, OutputInterface $output) { $loop = Factory::create(); $pusher = $this->getContainer()->get('gameList'); $context = new Context($loop); $pull = $context->getSocket(\ZMQ::SOCKET_PULL); $pull->bind('tcp://127.0.0.1:5555'); $pull->on('message', [$pusher, 'onBlogEntry']); $webSock = new Server($loop); $webSock->listen(8081, '0.0.0.0'); $webServer = new IoServer(new HttpServer(new WsServer(new WampServer($pusher))), $webSock); $loop->run(); }