Skip to content

Commit

Permalink
Merge pull request #33 from GeniusesOfSymfony/websocket-load-balance
Browse files Browse the repository at this point in the history
Allow to load balance websocket
  • Loading branch information
Johann Saunier committed Jul 5, 2015
2 parents 49b1851 + 6952471 commit 6d66723
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 43 deletions.
34 changes: 30 additions & 4 deletions Command/WebsocketServerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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')
;
}

/**
Expand All @@ -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')
);
}
}
6 changes: 4 additions & 2 deletions Resources/config/services/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Server/EntryPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -46,6 +46,6 @@ public function launch($serverName, $profile)
$server = $servers[$serverName];
}

$server->launch($profile);
$server->launch($host, $port, $profile);
}
}
9 changes: 1 addition & 8 deletions Server/Type/ServerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 3 additions & 27 deletions Server/Type/WebSocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ class WebSocketServer implements ServerInterface
/** @var LoopInterface */
protected $loop;

/**
* @var string
*/
protected $host;

/**
* @var int
*/
protected $port;

/**
* @var \SessionHandler|null
*/
Expand Down Expand Up @@ -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
Expand All @@ -83,8 +71,6 @@ class WebSocketServer implements ServerInterface
*/
public function __construct(
LoopInterface $loop,
$host,
$port,
EventDispatcherInterface $eventDispatcher,
PeriodicRegistry $periodicRegistry,
WampApplication $wampApplication,
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 6d66723

Please sign in to comment.