Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
handshake middleware was added for server
  • Loading branch information
valentint committed Feb 17, 2017
commit 5df35d68bd0681aa331531b23fa290b23f18bc1d
14 changes: 14 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('handshake_middleware')
->prototype('array')
->children()
->scalarNode('class')
->end()
->arrayNode('arguments')
->prototype('scalar')
->example('@some_service')
->end()
->end()
->end()
->example('{class: Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface, arguments: []}')
->end()
->end()
->end()
->end()
->arrayNode('rpc')
Expand Down
14 changes: 14 additions & 0 deletions DependencyInjection/GosWebSocketExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ public function load(array $configs, ContainerBuilder $container)
}
}

$HandshakeMiddlewareRegistryDef = $container->getDefinition('gos_web_socket.handshake_middleware.registry');
if (!empty($configs['server']['handshake_middleware'])) {
foreach ($configs['server']['handshake_middleware'] as $middleware) {
$arguments = [];
foreach ($middleware['arguments'] as $argument) {
$arguments[] = new Reference(ltrim($argument, '@'));
}
$middleware['arguments'] = $arguments;
$HandshakeMiddlewareRegistryDef->addMethodCall('addMiddleware', [$middleware]);
}
}



$container->setParameter('web_socket_server.client_storage.ttl', $configs['client']['storage']['ttl']);
$container->setParameter('web_socket_server.client_storage.prefix', $configs['client']['storage']['prefix']);

Expand Down
4 changes: 4 additions & 0 deletions Resources/config/services/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ services:
- '@gos_web_socket.wamp.topic_manager'
- '@gos_web_socket.server_push_handler.registry'
- '@?monolog.logger.websocket'
- '@gos_web_socket.handshake_middleware.registry'
tags:
- { name: gos_web_socket.server }

Expand Down Expand Up @@ -85,6 +86,9 @@ services:
gos_web_socket.origins.registry:
class: Gos\Bundle\WebSocketBundle\Server\App\Registry\OriginRegistry

gos_web_socket.handshake_middleware.registry:
class: Gos\Bundle\WebSocketBundle\Server\App\Registry\HandshakeMiddlewareRegistry

gos_web_socket_server.wamp_application:
class: Gos\Bundle\WebSocketBundle\Server\App\WampApplication
public: false
Expand Down
3 changes: 3 additions & 0 deletions Resources/docs/ConfigurationReference.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ gos_web_socket:
- @AcmeBundle/Resources/config/pubsub/routing.yml
context:
tokenSeparator: "/"
handshake_middleware: []
# {class: Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface, arguments: ['@some_service']}

rpc: []
topics: []
periodic: []
Expand Down
47 changes: 47 additions & 0 deletions Server/App/Registry/HandshakeMiddlewareRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Gos\Bundle\WebSocketBundle\Server\App\Registry;

use Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface;
use Ratchet\Http\HttpServerInterface;

/**
* @author Tkachew <[email protected]>
*/
class HandshakeMiddlewareRegistry
{
/**
* @var []
*/
protected $middlewares;

public function __construct()
{
$this->middlewares = [];
}

/**
* @param array $middleware
* @throws \Exception
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where?

*/
public function addMiddleware($middleware)
{
$interfaces = class_implements($middleware['class']);

if (!isset($interfaces['Ratchet\Http\HttpServerInterface'])) {
throw new \Exception("'Ratchet\\Http\\HttpServerInterface' in not implemented by '{$middleware['class']}'");
}

$arguments = array_merge([$middleware['class']], $middleware['arguments']);

$this->middlewares[] = $arguments;
}

/**
* @return []
*/
public function getMiddlewares()
{
return $this->middlewares;
}
}
13 changes: 13 additions & 0 deletions Server/App/Stack/HandshakeMiddlewareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Gos\Bundle\WebSocketBundle\Server\App\Stack;

use Ratchet\Http\HttpServerInterface;


/**
* @author Tkachew <[email protected]>
*/
interface HandshakeMiddlewareInterface extends HttpServerInterface
{
}
16 changes: 15 additions & 1 deletion Server/Type/WebSocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Gos\Bundle\WebSocketBundle\Periodic\PeriodicInterface;
use Gos\Bundle\WebSocketBundle\Periodic\PeriodicMemoryUsage;
use Gos\Bundle\WebSocketBundle\Pusher\ServerPushHandlerRegistry;
use Gos\Bundle\WebSocketBundle\Server\App\Registry\HandshakeMiddlewareRegistry;
use Gos\Bundle\WebSocketBundle\Server\App\Registry\OriginRegistry;
use Gos\Bundle\WebSocketBundle\Server\App\Registry\PeriodicRegistry;
use Gos\Bundle\WebSocketBundle\Server\App\WampApplication;
Expand Down Expand Up @@ -70,6 +71,9 @@ class WebSocketServer implements ServerInterface
/** @var TopicManager */
protected $topicManager;

/** @var HandshakeMiddlewareRegistry */
protected $handshakeMiddlewareRegistry;

/**
* @param LoopInterface $loop
* @param EventDispatcherInterface $eventDispatcher
Expand All @@ -79,6 +83,7 @@ class WebSocketServer implements ServerInterface
* @param bool $originCheck
* @param TopicManager $topicManager
* @param LoggerInterface|null $logger
* @param HandshakeMiddlewareRegistry $handshakeMiddlewareRegistry
*/
public function __construct(
LoopInterface $loop,
Expand All @@ -89,7 +94,8 @@ public function __construct(
$originCheck,
TopicManager $topicManager,
ServerPushHandlerRegistry $serverPushHandlerRegistry,
LoggerInterface $logger = null
LoggerInterface $logger = null,
HandshakeMiddlewareRegistry $handshakeMiddlewareRegistry

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguments with default values must be at the end of the argument list

) {
$this->loop = $loop;
$this->eventDispatcher = $eventDispatcher;
Expand All @@ -101,6 +107,7 @@ public function __construct(
$this->topicManager = $topicManager;
$this->serverPusherHandlerRegistry = $serverPushHandlerRegistry;
$this->sessionHandler = new NullSessionHandler();
$this->handshakeMiddlewareRegistry = $handshakeMiddlewareRegistry;
}

/**
Expand Down Expand Up @@ -154,6 +161,13 @@ public function launch($host, $port, $profile)
$stack->push('Gos\Bundle\WebSocketBundle\Server\App\Stack\OriginCheck', $allowedOrigins, $this->eventDispatcher);
}


if (!empty($this->handshakeMiddlewareRegistry->getMiddlewares())) {
foreach ($this->handshakeMiddlewareRegistry->getMiddlewares() as $middleware) {
call_user_func([$stack, 'push'], $middleware);
}
}

$stack
->push('Ratchet\WebSocket\WsServer')
->push('Gos\Bundle\WebSocketBundle\Server\App\Stack\WampConnectionPeriodicTimer', $this->loop)
Expand Down