Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose RFC6455 permessage-deflate #851

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 45 additions & 1 deletion src/Ratchet/WebSocket/WsConnection.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,50 @@
<?php
namespace Ratchet\WebSocket;
use Ratchet\AbstractConnectionDecorator;
use Ratchet\ConnectionInterface;
use Ratchet\RFC6455\Handshake\PermessageDeflateOptions;
use Ratchet\RFC6455\Messaging\CloseFrameChecker;
use Ratchet\RFC6455\Messaging\DataInterface;
use Ratchet\RFC6455\Messaging\Frame;
use Ratchet\RFC6455\Messaging\FrameInterface;
use Ratchet\RFC6455\Messaging\MessageBuffer;
use Ratchet\RFC6455\Messaging\MessageInterface;

/**
* {@inheritdoc}
* @property \StdClass $WebSocket
*/
class WsConnection extends AbstractConnectionDecorator {
/** @var MessageBuffer */
private $streamer;

public function __construct(ConnectionInterface $conn, callable $onMessage, callable $onControlFrame, PermessageDeflateOptions $pmdOptions = null) {
parent::__construct($conn);

$closeFrameChecker = new CloseFrameChecker();

$reusableUnderflowException = new \UnderflowException;

$this->streamer = new MessageBuffer(
$closeFrameChecker,
function(MessageInterface $msg) use ($onMessage) {
$onMessage($this, $msg);
},
function(FrameInterface $frame) use ($onControlFrame) {
$onControlFrame($frame, $this);
},
true,
function() use ($reusableUnderflowException) {
return $reusableUnderflowException;
},
null,
null,
[$conn, 'send'],
$pmdOptions
);
}


/**
* {@inheritdoc}
*/
Expand All @@ -18,7 +54,7 @@ public function send($msg) {
$msg = new Frame($msg);
}

$this->getConnection()->send($msg->getContents());
$this->streamer->sendFrame($msg);
}

return $this;
Expand All @@ -42,4 +78,12 @@ public function close($code = 1000) {

$this->WebSocket->closing = true;
}

/**
* @return MessageBuffer
*/
public function getStreamer()
{
return $this->streamer;
}
}
39 changes: 12 additions & 27 deletions src/Ratchet/WebSocket/WsServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use Ratchet\Http\HttpServerInterface;
use Ratchet\Http\CloseResponseTrait;
use Psr\Http\Message\RequestInterface;
use Ratchet\RFC6455\Handshake\PermessageDeflateOptions;
use Ratchet\RFC6455\Messaging\MessageInterface;
use Ratchet\RFC6455\Messaging\FrameInterface;
use Ratchet\RFC6455\Messaging\Frame;
use Ratchet\RFC6455\Messaging\MessageBuffer;
use Ratchet\RFC6455\Messaging\CloseFrameChecker;
use Ratchet\RFC6455\Handshake\ServerNegotiator;
use Ratchet\RFC6455\Handshake\RequestVerifier;
Expand Down Expand Up @@ -46,11 +46,6 @@ class WsServer implements HttpServerInterface {
*/
private $handshakeNegotiator;

/**
* @var \Closure
*/
private $ueFlowFactory;

/**
* @var \Closure
*/
Expand All @@ -63,9 +58,10 @@ class WsServer implements HttpServerInterface {

/**
* @param \Ratchet\WebSocket\MessageComponentInterface|\Ratchet\MessageComponentInterface $component Your application to run with WebSockets
* @param bool $enablePermessageDeflate
* @note If you want to enable sub-protocols have your component implement WsServerInterface as well
*/
public function __construct(ComponentInterface $component) {
public function __construct(ComponentInterface $component, $enablePermessageDeflate = false) {
if ($component instanceof MessageComponentInterface) {
$this->msgCb = function(ConnectionInterface $conn, MessageInterface $msg) {
$this->delegate->onMessage($conn, $msg);
Expand All @@ -86,19 +82,14 @@ public function __construct(ComponentInterface $component) {
$this->connections = new \SplObjectStorage;

$this->closeFrameChecker = new CloseFrameChecker;
$this->handshakeNegotiator = new ServerNegotiator(new RequestVerifier);
$this->handshakeNegotiator = new ServerNegotiator(new RequestVerifier, $enablePermessageDeflate);
$this->handshakeNegotiator->setStrictSubProtocolCheck(true);

if ($component instanceof WsServerInterface) {
$this->handshakeNegotiator->setSupportedSubProtocols($component->getSubProtocols());
}

$this->pongReceiver = function() {};

$reusableUnderflowException = new \UnderflowException;
$this->ueFlowFactory = function() use ($reusableUnderflowException) {
return $reusableUnderflowException;
};
}

/**
Expand All @@ -122,22 +113,16 @@ public function onOpen(ConnectionInterface $conn, RequestInterface $request = nu
return $conn->close();
}

$wsConn = new WsConnection($conn);

$streamer = new MessageBuffer(
$this->closeFrameChecker,
function(MessageInterface $msg) use ($wsConn) {
$cb = $this->msgCb;
$cb($wsConn, $msg);
},
function(FrameInterface $frame) use ($wsConn) {
$this->onControlFrame($frame, $wsConn);
},
true,
$this->ueFlowFactory
$pmdOptions = PermessageDeflateOptions::fromRequestOrResponse($response);

$wsConn = new WsConnection(
$conn,
$this->msgCb,
[$this, 'onControlFrame'],
$pmdOptions[0]
);

$this->connections->attach($conn, new ConnContext($wsConn, $streamer));
$this->connections->attach($conn, new ConnContext($wsConn, $wsConn->getStreamer()));

return $this->delegate->onOpen($wsConn);
}
Expand Down