Skip to content

Commit d5e19a5

Browse files
author
prophet777
committed
Dev tools, more docs, secure topic against exception
1 parent b096438 commit d5e19a5

File tree

15 files changed

+289
-94
lines changed

15 files changed

+289
-94
lines changed

Client/ClientStorage.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function getClient($identifier)
4646
{
4747
$result = $this->driver->fetch($identifier);
4848

49-
$this->logger->debug('GET CLIENT '.$identifier);
49+
$this->logger->debug('GET CLIENT ' . $identifier);
5050

5151
if (false === $result) {
5252
throw new StorageException(sprintf('Client %s not found', $identifier));
@@ -68,17 +68,17 @@ public static function getStorageId(ConnectionInterface $conn)
6868
*/
6969
public function addClient($identifier, $user)
7070
{
71-
$serializedUser = serialize($user);
71+
$serializedUser = serialize($user);
7272

7373
$context = [
74-
'user' => $serializedUser
74+
'user' => $serializedUser,
7575
];
7676

77-
if($user instanceof UserInterface){
77+
if ($user instanceof UserInterface) {
7878
$context['username'] = $user->getUsername();
7979
}
8080

81-
$this->logger->debug(sprintf('INSERT CLIENT '. $identifier), $context);
81+
$this->logger->debug(sprintf('INSERT CLIENT ' . $identifier), $context);
8282

8383
if (false === $result = $this->driver->save($identifier, $serializedUser, 60 * 15)) {
8484
throw new StorageException('Unable add client');
@@ -98,7 +98,7 @@ public function hasClient($identifier)
9898
*/
9999
public function removeClient($identifier)
100100
{
101-
$this->logger->debug('REMOVE CLIENT '.$identifier);
101+
$this->logger->debug('REMOVE CLIENT ' . $identifier);
102102

103103
return $this->driver->delete($identifier);
104104
}

Client/WebSocketUserTrait.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Gos\Bundle\WebSocketBundle\Client;
4+
5+
use Ratchet\ConnectionInterface;
6+
7+
trait WebSocketUserTrait
8+
{
9+
/**
10+
* @var ClientStorageInterface
11+
*/
12+
protected $clientStorage;
13+
14+
/**
15+
* @param ConnectionInterface $connection
16+
*
17+
* @return false|string|\Symfony\Component\Security\Core\User\UserInterface
18+
*/
19+
public function getCurrentUser(ConnectionInterface $connection)
20+
{
21+
return $this->clientStorage->getClient($this->clientStorage->getStorageId($connection));
22+
}
23+
}

Command/ServerCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace Gos\Bundle\WebSocketBundle\Command;
44

5-
use Psr\Log\LoggerInterface;
65
use Symfony\Component\Console\Input\InputArgument;
76
use Symfony\Component\Console\Input\InputInterface;
87
use Symfony\Component\Console\Output\OutputInterface;
98

109
/**
1110
* @author Johann Saunier <[email protected]>
11+
*
1212
* @deprecated Will be removed in v2 use WebsocketServerCommand instead
1313
*/
1414
class ServerCommand extends WebsocketServerCommand

Command/WebsocketServerCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ protected function execute(InputInterface $input, OutputInterface $output)
5050
{
5151
$this->entryPoint->launch($input->getArgument('name'));
5252
}
53-
}
53+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Gos\Bundle\WebSocketBundle\DependencyInjection\CompilerPass;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Reference;
8+
9+
class DevCompilerPass implements CompilerPassInterface
10+
{
11+
/**
12+
* @param ContainerBuilder $container
13+
*/
14+
public function process(ContainerBuilder $container)
15+
{
16+
if (false === $container->getParameter('kernel.debug')) {
17+
return;
18+
}
19+
20+
$periodicRegistryDef = $container->getDefinition('gos_web_socket.periodic.registry');
21+
22+
$periodicRegistryDef->addMethodCall(
23+
'addPeriodic', [new Reference('gos_web_socket.memory_usage.periodic')]
24+
);
25+
}
26+
}

DependencyInjection/CompilerPass/PingableDriverCompilerPass.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ public function process(ContainerBuilder $container)
1717
{
1818
$sessionHandler = $container->get('gos_web_socket.session_handler', Container::NULL_ON_INVALID_REFERENCE);
1919

20-
if(null === $sessionHandler && false === $sessionHandler instanceof PdoSessionHandler){
20+
if (null === $sessionHandler && false === $sessionHandler instanceof PdoSessionHandler) {
2121
return;
2222
}
2323

2424
$periodicRegistryDef = $container->getDefinition('gos_web_socket.periodic.registry');
2525

26-
if(false === $container->hasParameter('database_driver')){
26+
if (false === $container->hasParameter('database_driver')) {
2727
return;
2828
}
2929

3030
$pdoDriver = ['pdo_mysql', 'pdo_sqlite', 'pdo_pgsql'];
3131

32-
if(in_array($container->getParameter('database_driver'), $pdoDriver)){
32+
if (in_array($container->getParameter('database_driver'), $pdoDriver)) {
3333
$periodicRegistryDef->addMethodCall(
3434
'addPeriodic', [new Reference('gos_web_socket.pdo.periodic_ping')]
3535
);
3636
}
3737
}
38-
}
38+
}

Event/ClientEventListener.php

Lines changed: 53 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Gos\Bundle\WebSocketBundle\Client\ClientStorageInterface;
66
use Gos\Bundle\WebSocketBundle\Client\StorageException;
77
use Psr\Log\LoggerInterface;
8+
use Symfony\Component\HttpKernel\Log\NullLogger;
89
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
910
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1011
use Symfony\Component\Security\Core\SecurityContextInterface;
@@ -57,7 +58,7 @@ public function __construct(
5758
$this->clientStorage = $clientStorage;
5859
$this->firewalls = $firewalls;
5960
$this->securityContext = $securityContext;
60-
$this->logger = $logger;
61+
$this->logger = null === $logger ? new NullLogger() : $logger;
6162
$this->originChecker = $originChecker;
6263
}
6364

@@ -75,12 +76,10 @@ public function onClientConnect(ClientEvent $event)
7576
$this->logger->warning(sprintf('User firewall is not configured, we have set %s by default', $this->firewalls[0]));
7677
}
7778

78-
if (null !== $this->logger) {
79-
$loggerContext = array(
80-
'connection_id' => $conn->resourceId,
81-
'session_id' => $conn->WAMP->sessionId,
82-
);
83-
}
79+
$loggerContext = array(
80+
'connection_id' => $conn->resourceId,
81+
'session_id' => $conn->WAMP->sessionId,
82+
);
8483

8584
$token = null;
8685

@@ -107,27 +106,22 @@ public function onClientConnect(ClientEvent $event)
107106
: $user;
108107

109108
try {
110-
$className = get_class($this->clientStorage);
111-
$identifier = $className::getStorageId($conn, $username);
109+
$identifier = $this->clientStorage->getStorageId($conn, $username);
112110
$loggerContext['storage_id'] = $identifier;
113111

114112
$this->clientStorage->addClient($identifier, $user);
115113
$conn->WAMP->clientStorageId = $identifier;
116114

117-
if (null !== $this->logger) {
118-
$this->logger->info(sprintf(
119-
'%s connected [%]',
120-
$username,
121-
$user instanceof UserInterface ? implode(', ', $user->getRoles()) : array()
122-
), $loggerContext);
123-
}
115+
$this->logger->info(sprintf(
116+
'%s connected [%]',
117+
$username,
118+
$user instanceof UserInterface ? implode(', ', $user->getRoles()) : array()
119+
), $loggerContext);
124120
} catch (StorageException $e) {
125-
if (null !== $this->logger) {
126-
$this->logger->error(
127-
$e->getMessage(),
128-
$loggerContext
129-
);
130-
}
121+
$this->logger->error(
122+
$e->getMessage(),
123+
$loggerContext
124+
);
131125

132126
throw $e;
133127
}
@@ -149,29 +143,27 @@ public function onClientDisconnect(ClientEvent $event)
149143
? $user->getUsername()
150144
: $user;
151145

152-
if (null !== $this->logger) {
153-
$this->logger->info(sprintf(
154-
'%s disconnected [%]',
155-
$username,
156-
$user instanceof UserInterface ? implode(', ', $user->getRoles()) : array()
157-
), array(
158-
'connection_id' => $conn->resourceId,
159-
'session_id' => $conn->WAMP->sessionId,
160-
'storage_id' => $conn->WAMP->clientStorageId,
161-
));
162-
}
146+
147+
$this->logger->info(sprintf(
148+
'%s disconnected [%]',
149+
$username,
150+
$user instanceof UserInterface ? implode(', ', $user->getRoles()) : array()
151+
), array(
152+
'connection_id' => $conn->resourceId,
153+
'session_id' => $conn->WAMP->sessionId,
154+
'storage_id' => $conn->WAMP->clientStorageId,
155+
));
156+
163157
} catch (StorageException $e) {
164-
if (null !== $this->logger) {
165-
$this->logger->info(sprintf(
166-
'%s disconnected [%s]',
167-
'Expired user',
168-
''
169-
), array(
170-
'connection_id' => $conn->resourceId,
171-
'session_id' => $conn->WAMP->sessionId,
172-
'storage_id' => $conn->WAMP->clientStorageId,
173-
));
174-
}
158+
$this->logger->info(sprintf(
159+
'%s disconnected [%s]',
160+
'Expired user',
161+
''
162+
), array(
163+
'connection_id' => $conn->resourceId,
164+
'session_id' => $conn->WAMP->sessionId,
165+
'storage_id' => $conn->WAMP->clientStorageId,
166+
));
175167
}
176168

177169
$this->clientStorage->removeClient($conn->resourceId);
@@ -187,34 +179,32 @@ public function onClientError(ClientErrorEvent $event)
187179
$conn = $event->getConnection();
188180
$e = $event->getException();
189181

190-
if (null !== $this->logger) {
191-
$loggerContext = array(
192-
'connection_id' => $conn->resourceId,
193-
'session_id' => $conn->WAMP->sessionId,
194-
);
195182

196-
if ($this->clientStorage->hasClient($conn->resourceId)) {
197-
$loggerContext['client'] = $this->clientStorage->getClient($conn->WAMP->clientStorageId);
198-
}
183+
$loggerContext = array(
184+
'connection_id' => $conn->resourceId,
185+
'session_id' => $conn->WAMP->sessionId,
186+
);
199187

200-
$this->logger->error(sprintf(
201-
'Connection error occurred %s in %s line %s',
202-
$e->getMessage(),
203-
$e->getFile(),
204-
$e->getLine()
205-
), $loggerContext);
188+
if ($this->clientStorage->hasClient($conn->resourceId)) {
189+
$loggerContext['client'] = $this->clientStorage->getClient($conn->WAMP->clientStorageId);
206190
}
191+
192+
$this->logger->error(sprintf(
193+
'Connection error occurred %s in %s line %s',
194+
$e->getMessage(),
195+
$e->getFile(),
196+
$e->getLine()
197+
), $loggerContext);
198+
207199
}
208200

209201
/**
210202
* @param ClientRejectedEvent $event
211203
*/
212204
public function onClientRejected(ClientRejectedEvent $event)
213205
{
214-
if (null !== $this->logger) {
215-
$this->logger->warning('Client rejected, bad origin', [
216-
'origin' => $event->getOrigin(),
217-
]);
218-
}
206+
$this->logger->warning('Client rejected, bad origin', [
207+
'origin' => $event->getOrigin(),
208+
]);
219209
}
220210
}

GosWebSocketBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Gos\Bundle\WebSocketBundle;
44

5+
use Gos\Bundle\WebSocketBundle\DependencyInjection\CompilerPass\DevCompilerPass;
56
use Gos\Bundle\WebSocketBundle\DependencyInjection\CompilerPass\PeriodicCompilerPass;
67
use Gos\Bundle\WebSocketBundle\DependencyInjection\CompilerPass\PingableDriverCompilerPass;
78
use Gos\Bundle\WebSocketBundle\DependencyInjection\CompilerPass\RpcCompilerPass;
@@ -25,5 +26,6 @@ public function build(ContainerBuilder $container)
2526
$container->addCompilerPass(new TopicCompilerPass());
2627
$container->addCompilerPass(new PeriodicCompilerPass());
2728
$container->addCompilerPass(new PingableDriverCompilerPass());
29+
$container->addCompilerPass(new DevCompilerPass());
2830
}
2931
}

Periodic/PdoPeriodicPing.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,23 @@ public function setTimeout($timeout)
4545

4646
public function tick()
4747
{
48-
if(null === $this->pdo){
48+
if (null === $this->pdo) {
4949
$this->logger->warning('Unable to ping sql server, service pdo is unavailable');
50+
5051
return;
5152
}
5253

5354
//if connection is persistent we don't need to ping
54-
if(true === $this->pdo->getAttribute(\PDO::ATTR_PERSISTENT)){
55+
if (true === $this->pdo->getAttribute(\PDO::ATTR_PERSISTENT)) {
5556
return;
5657
}
5758

58-
try{
59+
try {
5960
$startTime = microtime(true);
6061
$this->pdo->query('SELECT 1');
6162
$endTime = microtime(true);
6263
$this->logger->notice(sprintf('Successfully ping sql server (~%s ms)', round(($endTime - $startTime) * 100000), 2));
63-
}catch (\PDOException $e){
64+
} catch (\PDOException $e) {
6465
$this->logger->emergency('Sql server is gone, and unable to reconnect');
6566
throw $e;
6667
}
@@ -73,6 +74,4 @@ public function getTimeout()
7374
{
7475
return $this->timeout;
7576
}
76-
77-
78-
}
77+
}

0 commit comments

Comments
 (0)