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
Prev Previous commit
Next Next commit
middleware factory done
  • Loading branch information
valentint committed Feb 20, 2017
commit e2afa63a8a68d25a2905c208b36511856aed41d6
13 changes: 2 additions & 11 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,8 @@ public function getConfigTreeBuilder()
->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: []}')
->prototype('scalar')
->example('@some_service # implements Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface')
->end()
->end()
->end()
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Resources
* [Performance Bench](Resources/docs/Performance.md)
* [Push integration](Resources/docs/Pusher.md)
* [SSL configuration](Resources/docs/Ssl.md)
* [Handshake Middleware For Server](Resources/docs/HandshakeMiddleware.md)

Code Cookbook
--------------
Expand Down
2 changes: 1 addition & 1 deletion Resources/docs/ConfigurationReference.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ gos_web_socket:
context:
tokenSeparator: "/"
handshake_middleware: []
# - {class: Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface, arguments: ['@some_service']}
# - @some_service # implements Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface

rpc: []
topics: []
Expand Down
14 changes: 14 additions & 0 deletions Resources/docs/HandshakeMiddleware.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# HandshakeMiddleware

You can add any middleware as service to server with your business logic


**Bundle Configuration**

```yaml
# Gos Web Socket Bundle
gos_web_socket:
server:
handshake_middleware: []
- @some_service # implements Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface
```
34 changes: 9 additions & 25 deletions Server/App/Stack/Factory/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* @author Johann Saunier <[email protected]>
*/
class Middleware extends HttpServerInterface
class Middleware implements HttpServerInterface
{
/**
* @var \Ratchet\MessageComponentInterface
Expand Down Expand Up @@ -43,54 +43,38 @@ public function __construct(
*/
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null)
{
$this->_middleware->onOpen($conn, $request);
$answer = $this->_middleware->onOpen($conn, $request);

return $this->_component->onOpen($conn, $request);
return $answer instanceof ComponentInterface ? $answer : $this->_component->onOpen($conn, $request);
}

/**
* {@inheritdoc}
*/
public function onMessage(ConnectionInterface $from, $msg)
{
$this->_middleware->onMessage($from, $msg);
$answer = $this->_middleware->onMessage($from, $msg);

return $this->_component->onMessage($from, $msg);
return $answer instanceof MessageInterface ? $answer : $this->_component->onMessage($from, $msg);
}

/**
* {@inheritdoc}
*/
public function onClose(ConnectionInterface $conn)
{
$this->_middleware->onClose($conn);
$answer = $this->_middleware->onClose($conn);

return $this->_component->onClose($conn);
return $answer instanceof ComponentInterface ? $answer : $this->_component->onClose($conn);
}

/**
* {@inheritdoc}
*/
public function onError(ConnectionInterface $conn, \Exception $e)
{
$this->_middleware->onError($conn, $e);
$answer = $this->_middleware->onError($conn, $e);

return $this->_component->onError($conn, $e);
}

/**
* Close a connection with an HTTP response.
*
* @param \Ratchet\ConnectionInterface $conn
* @param int $code HTTP status code
*/
protected function close(ConnectionInterface $conn, $code = 400)
{
$response = new Response($code, array(
'X-Powered-By' => \Ratchet\VERSION,
));

$conn->send((string) $response);
$conn->close();
return $answer instanceof ComponentInterface ? $answer : $this->_component->onError($conn, $e);
}
}
2 changes: 1 addition & 1 deletion Server/Type/WebSocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function launch($host, $port, $profile)

if (!empty($this->handshakeMiddlewareRegistry->getMiddlewares())) {
foreach ($this->handshakeMiddlewareRegistry->getMiddlewares() as $middleware) {
call_user_func([$stack, 'push'], $middleware);
call_user_func([$stack, 'push'], ['Gos\Bundle\WebSocketBundle\Server\App\Stack\Factory\Middleware', $middleware]);
}
}

Expand Down