Skip to content

Commit 6d66723

Browse files
author
Johann Saunier
committed
Merge pull request #33 from GeniusesOfSymfony/websocket-load-balance
Allow to load balance websocket
2 parents 49b1851 + 6952471 commit 6d66723

File tree

5 files changed

+40
-43
lines changed

5 files changed

+40
-43
lines changed

Command/WebsocketServerCommand.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,31 @@ class WebsocketServerCommand extends Command
2323
*/
2424
protected $logger;
2525

26+
/**
27+
* @var string
28+
*/
29+
protected $host;
30+
31+
/**
32+
* @var int
33+
*/
34+
protected $port;
35+
2636
/**
2737
* @param EntryPoint $entryPoint
38+
* @param string $host
39+
* @param int $port
2840
* @param LoggerInterface $logger
2941
*/
30-
public function __construct(EntryPoint $entryPoint, LoggerInterface $logger = null)
31-
{
42+
public function __construct(
43+
EntryPoint $entryPoint,
44+
$host,
45+
$port,
46+
LoggerInterface $logger = null
47+
) {
3248
$this->entryPoint = $entryPoint;
49+
$this->port = (int) $port;
50+
$this->host = $host;
3351
$this->logger = null === $logger ? new NullLogger() : $logger;
3452

3553
parent::__construct();
@@ -41,7 +59,10 @@ protected function configure()
4159
->setName('gos:websocket:server')
4260
->setDescription('Starts the web socket servers')
4361
->addArgument('name', InputArgument::OPTIONAL, 'Server name')
44-
->addOption('profile', 'p', InputOption::VALUE_NONE, 'Profiling server');
62+
->addOption('profile', 'm', InputOption::VALUE_NONE, 'Profiling server')
63+
->addOption('host', 'a', InputOption::VALUE_OPTIONAL, 'Host')
64+
->addOption('port', 'p', InputOption::VALUE_OPTIONAL, 'port')
65+
;
4566
}
4667

4768
/**
@@ -50,6 +71,11 @@ protected function configure()
5071
*/
5172
protected function execute(InputInterface $input, OutputInterface $output)
5273
{
53-
$this->entryPoint->launch($input->getArgument('name'), $input->getOption('profile'));
74+
$this->entryPoint->launch(
75+
$input->getArgument('name'),
76+
$input->getOption('host') === null ? $this->host : $input->getOption('host'),
77+
$input->getOption('port') === null ? $this->port : $input->getOption('port'),
78+
$input->getOption('profile')
79+
);
5480
}
5581
}

Resources/config/services/services.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ services:
1414
class: Gos\Bundle\WebSocketBundle\Command\ServerCommand
1515
arguments:
1616
- @gos_web_socket.entry_point
17+
- %web_socket_server.host%
18+
- %web_socket_server.port%
1719
- @?monolog.logger.websocket
1820
tags:
1921
- { name: console.command }
@@ -22,6 +24,8 @@ services:
2224
class: Gos\Bundle\WebSocketBundle\Command\WebsocketServerCommand
2325
arguments:
2426
- @gos_web_socket.entry_point
27+
- %web_socket_server.host%
28+
- %web_socket_server.port%
2529
- @?monolog.logger.websocket
2630
tags:
2731
- { name: console.command }
@@ -40,8 +44,6 @@ services:
4044
lazy: true
4145
arguments:
4246
- @gos_web_socket.server.event_loop
43-
- %web_socket_server.host%
44-
- %web_socket_server.port%
4547
- @event_dispatcher
4648
- @gos_web_socket.periodic.registry
4749
- @gos_web_socket_server.wamp_application

Server/EntryPoint.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(ServerRegistry $serverRegistry)
2727
* @param string $serverName
2828
* @param bool $profile
2929
*/
30-
public function launch($serverName, $profile)
30+
public function launch($serverName, $host, $port, $profile)
3131
{
3232
$servers = $this->serverRegistry->getServers();
3333

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

49-
$server->launch($profile);
49+
$server->launch($host, $port, $profile);
5050
}
5151
}

Server/Type/ServerInterface.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,7 @@ interface ServerInterface
77
/**
88
* Launches the server loop.
99
*/
10-
public function launch($profile);
11-
12-
/**
13-
* Returns a string of the host:port for debugging / display purposes.
14-
*
15-
* @return string
16-
*/
17-
public function getAddress();
10+
public function launch($host, $port, $profile);
1811

1912
/**
2013
* Returns a string of the name of the server/service for debugging / display purposes.

Server/Type/WebSocketServer.php

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ class WebSocketServer implements ServerInterface
2626
/** @var LoopInterface */
2727
protected $loop;
2828

29-
/**
30-
* @var string
31-
*/
32-
protected $host;
33-
34-
/**
35-
* @var int
36-
*/
37-
protected $port;
38-
3929
/**
4030
* @var \SessionHandler|null
4131
*/
@@ -72,8 +62,6 @@ class WebSocketServer implements ServerInterface
7262
protected $logger;
7363

7464
/**
75-
* @param string $host
76-
* @param int $port
7765
* @param EventDispatcherInterface $eventDispatcher
7866
* @param PeriodicRegistry $periodicRegistry
7967
* @param WampApplication $wampApplication
@@ -83,8 +71,6 @@ class WebSocketServer implements ServerInterface
8371
*/
8472
public function __construct(
8573
LoopInterface $loop,
86-
$host,
87-
$port,
8874
EventDispatcherInterface $eventDispatcher,
8975
PeriodicRegistry $periodicRegistry,
9076
WampApplication $wampApplication,
@@ -93,8 +79,6 @@ public function __construct(
9379
LoggerInterface $logger = null
9480
) {
9581
$this->loop = $loop;
96-
$this->host = $host;
97-
$this->port = $port;
9882
$this->eventDispatcher = $eventDispatcher;
9983
$this->periodicRegistry = $periodicRegistry;
10084
$this->wampApplication = $wampApplication;
@@ -117,14 +101,14 @@ public function setSessionHandler(\SessionHandlerInterface $sessionHandler)
117101
*
118102
* @throws \React\Socket\ConnectionException
119103
*/
120-
public function launch($profile)
104+
public function launch($host, $port, $profile)
121105
{
122106
$this->logger->info('Starting web socket');
123107

124108
$stack = new Builder();
125109

126110
$server = new Server($this->loop);
127-
$server->listen($this->port, $this->host);
111+
$server->listen($port, $host);
128112

129113
if (true === $profile) {
130114
$memoryUsagePeriodicTimer = new PeriodicMemoryUsage($this->logger);
@@ -167,21 +151,13 @@ public function launch($profile)
167151
$this->logger->info(sprintf(
168152
'Launching %s on %s PID: %s',
169153
$this->getName(),
170-
$this->getAddress(),
154+
$host.':'.$port,
171155
getmypid()
172156
));
173157

174158
$app->run();
175159
}
176160

177-
/**
178-
* @return string
179-
*/
180-
public function getAddress()
181-
{
182-
return $this->host . ':' . $this->port;
183-
}
184-
185161
/**
186162
* @return string
187163
*/

0 commit comments

Comments
 (0)