From 695247172567a95fcb2e4b58ddeeb7d6f19909de Mon Sep 17 00:00:00 2001 From: prophet777 Date: Sun, 5 Jul 2015 12:22:57 +0200 Subject: [PATCH] Allow to load balance websocket --- Command/WebsocketServerCommand.php | 34 +++++++++++++++++++++++--- Resources/config/services/services.yml | 6 +++-- Server/EntryPoint.php | 4 +-- Server/Type/ServerInterface.php | 9 +------ Server/Type/WebSocketServer.php | 30 +++-------------------- 5 files changed, 40 insertions(+), 43 deletions(-) diff --git a/Command/WebsocketServerCommand.php b/Command/WebsocketServerCommand.php index 0a782246..cc4658cd 100644 --- a/Command/WebsocketServerCommand.php +++ b/Command/WebsocketServerCommand.php @@ -23,13 +23,31 @@ class WebsocketServerCommand extends Command */ protected $logger; + /** + * @var string + */ + protected $host; + + /** + * @var int + */ + protected $port; + /** * @param EntryPoint $entryPoint + * @param string $host + * @param int $port * @param LoggerInterface $logger */ - public function __construct(EntryPoint $entryPoint, LoggerInterface $logger = null) - { + public function __construct( + EntryPoint $entryPoint, + $host, + $port, + LoggerInterface $logger = null + ) { $this->entryPoint = $entryPoint; + $this->port = (int) $port; + $this->host = $host; $this->logger = null === $logger ? new NullLogger() : $logger; parent::__construct(); @@ -41,7 +59,10 @@ protected function configure() ->setName('gos:websocket:server') ->setDescription('Starts the web socket servers') ->addArgument('name', InputArgument::OPTIONAL, 'Server name') - ->addOption('profile', 'p', InputOption::VALUE_NONE, 'Profiling server'); + ->addOption('profile', 'm', InputOption::VALUE_NONE, 'Profiling server') + ->addOption('host', 'a', InputOption::VALUE_OPTIONAL, 'Host') + ->addOption('port', 'p', InputOption::VALUE_OPTIONAL, 'port') + ; } /** @@ -50,6 +71,11 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - $this->entryPoint->launch($input->getArgument('name'), $input->getOption('profile')); + $this->entryPoint->launch( + $input->getArgument('name'), + $input->getOption('host') === null ? $this->host : $input->getOption('host'), + $input->getOption('port') === null ? $this->port : $input->getOption('port'), + $input->getOption('profile') + ); } } diff --git a/Resources/config/services/services.yml b/Resources/config/services/services.yml index ef0bbaad..40aa5578 100644 --- a/Resources/config/services/services.yml +++ b/Resources/config/services/services.yml @@ -14,6 +14,8 @@ services: class: Gos\Bundle\WebSocketBundle\Command\ServerCommand arguments: - @gos_web_socket.entry_point + - %web_socket_server.host% + - %web_socket_server.port% - @?monolog.logger.websocket tags: - { name: console.command } @@ -22,6 +24,8 @@ services: class: Gos\Bundle\WebSocketBundle\Command\WebsocketServerCommand arguments: - @gos_web_socket.entry_point + - %web_socket_server.host% + - %web_socket_server.port% - @?monolog.logger.websocket tags: - { name: console.command } @@ -40,8 +44,6 @@ services: lazy: true arguments: - @gos_web_socket.server.event_loop - - %web_socket_server.host% - - %web_socket_server.port% - @event_dispatcher - @gos_web_socket.periodic.registry - @gos_web_socket_server.wamp_application diff --git a/Server/EntryPoint.php b/Server/EntryPoint.php index 9ade0103..5b19e75e 100644 --- a/Server/EntryPoint.php +++ b/Server/EntryPoint.php @@ -27,7 +27,7 @@ public function __construct(ServerRegistry $serverRegistry) * @param string $serverName * @param bool $profile */ - public function launch($serverName, $profile) + public function launch($serverName, $host, $port, $profile) { $servers = $this->serverRegistry->getServers(); @@ -46,6 +46,6 @@ public function launch($serverName, $profile) $server = $servers[$serverName]; } - $server->launch($profile); + $server->launch($host, $port, $profile); } } diff --git a/Server/Type/ServerInterface.php b/Server/Type/ServerInterface.php index 0ee3cc71..40594be7 100644 --- a/Server/Type/ServerInterface.php +++ b/Server/Type/ServerInterface.php @@ -7,14 +7,7 @@ interface ServerInterface /** * Launches the server loop. */ - public function launch($profile); - - /** - * Returns a string of the host:port for debugging / display purposes. - * - * @return string - */ - public function getAddress(); + public function launch($host, $port, $profile); /** * Returns a string of the name of the server/service for debugging / display purposes. diff --git a/Server/Type/WebSocketServer.php b/Server/Type/WebSocketServer.php index 3c5a2f4d..8ff1c700 100644 --- a/Server/Type/WebSocketServer.php +++ b/Server/Type/WebSocketServer.php @@ -26,16 +26,6 @@ class WebSocketServer implements ServerInterface /** @var LoopInterface */ protected $loop; - /** - * @var string - */ - protected $host; - - /** - * @var int - */ - protected $port; - /** * @var \SessionHandler|null */ @@ -72,8 +62,6 @@ class WebSocketServer implements ServerInterface protected $logger; /** - * @param string $host - * @param int $port * @param EventDispatcherInterface $eventDispatcher * @param PeriodicRegistry $periodicRegistry * @param WampApplication $wampApplication @@ -83,8 +71,6 @@ class WebSocketServer implements ServerInterface */ public function __construct( LoopInterface $loop, - $host, - $port, EventDispatcherInterface $eventDispatcher, PeriodicRegistry $periodicRegistry, WampApplication $wampApplication, @@ -93,8 +79,6 @@ public function __construct( LoggerInterface $logger = null ) { $this->loop = $loop; - $this->host = $host; - $this->port = $port; $this->eventDispatcher = $eventDispatcher; $this->periodicRegistry = $periodicRegistry; $this->wampApplication = $wampApplication; @@ -117,14 +101,14 @@ public function setSessionHandler(\SessionHandlerInterface $sessionHandler) * * @throws \React\Socket\ConnectionException */ - public function launch($profile) + public function launch($host, $port, $profile) { $this->logger->info('Starting web socket'); $stack = new Builder(); $server = new Server($this->loop); - $server->listen($this->port, $this->host); + $server->listen($port, $host); if (true === $profile) { $memoryUsagePeriodicTimer = new PeriodicMemoryUsage($this->logger); @@ -167,21 +151,13 @@ public function launch($profile) $this->logger->info(sprintf( 'Launching %s on %s PID: %s', $this->getName(), - $this->getAddress(), + $host.':'.$port, getmypid() )); $app->run(); } - /** - * @return string - */ - public function getAddress() - { - return $this->host . ':' . $this->port; - } - /** * @return string */