From 2283bdf28856db73eda842accdd584256ccdd0e8 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Wed, 24 Feb 2016 18:55:04 -0500 Subject: [PATCH 1/6] Binary messaging support Least obtrusive interface --- src/Ratchet/WebSocket/BinaryMessageInterface.php | 8 ++++++++ src/Ratchet/WebSocket/WsConnection.php | 4 ++-- tests/autobahn/fuzzingclient-quick.json | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 src/Ratchet/WebSocket/BinaryMessageInterface.php diff --git a/src/Ratchet/WebSocket/BinaryMessageInterface.php b/src/Ratchet/WebSocket/BinaryMessageInterface.php new file mode 100644 index 0000000..db28c0b --- /dev/null +++ b/src/Ratchet/WebSocket/BinaryMessageInterface.php @@ -0,0 +1,8 @@ +WebSocket->closing) { if (!($msg instanceof DataInterface)) { - $msg = new Frame($msg); + $msg = new Frame($msg, true, ((boolean)$isBinary ? Frame::OP_TEXT : Frame::OP_BINARY)); } $this->getConnection()->send($msg->getContents()); diff --git a/tests/autobahn/fuzzingclient-quick.json b/tests/autobahn/fuzzingclient-quick.json index 3023fb8..c92e805 100644 --- a/tests/autobahn/fuzzingclient-quick.json +++ b/tests/autobahn/fuzzingclient-quick.json @@ -7,6 +7,6 @@ ] , "cases": ["*"] - , "exclude-cases": ["1.2.*", "2.3", "2.4", "2.6", "9.2.*", "9.4.*", "9.6.*", "9.8.*"] + , "exclude-cases": [] , "exclude-agent-cases": {} } From bbced3b7657e16254ce1af1a689b7aed3af55475 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sat, 27 Feb 2016 13:02:56 -0500 Subject: [PATCH 2/6] Fixed frame creation type bug --- src/Ratchet/WebSocket/WsConnection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Ratchet/WebSocket/WsConnection.php b/src/Ratchet/WebSocket/WsConnection.php index 4082fa9..3c34c9f 100644 --- a/src/Ratchet/WebSocket/WsConnection.php +++ b/src/Ratchet/WebSocket/WsConnection.php @@ -15,7 +15,7 @@ class WsConnection extends AbstractConnectionDecorator { public function send($msg, $isBinary = false) { if (!$this->WebSocket->closing) { if (!($msg instanceof DataInterface)) { - $msg = new Frame($msg, true, ((boolean)$isBinary ? Frame::OP_TEXT : Frame::OP_BINARY)); + $msg = new Frame($msg, true, ((boolean)$isBinary ? Frame::OP_BINARY : Frame::OP_TEXT)); } $this->getConnection()->send($msg->getContents()); From 365e8702fff6b65353c951785a68ca996c1e922b Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sat, 27 Feb 2016 13:03:12 -0500 Subject: [PATCH 3/6] WebSocket Binary message parent change --- src/Ratchet/WebSocket/BinaryMessageInterface.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Ratchet/WebSocket/BinaryMessageInterface.php b/src/Ratchet/WebSocket/BinaryMessageInterface.php index db28c0b..e5dc022 100644 --- a/src/Ratchet/WebSocket/BinaryMessageInterface.php +++ b/src/Ratchet/WebSocket/BinaryMessageInterface.php @@ -1,8 +1,8 @@ Date: Sat, 27 Feb 2016 13:04:10 -0500 Subject: [PATCH 4/6] Pass message value, isBinary indicator Passing message contents instead of string to keep BC Passing isBinary indicator regardless of interface --- src/Ratchet/WebSocket/WsServer.php | 2 +- tests/autobahn/bin/fuzzingserver.php | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Ratchet/WebSocket/WsServer.php b/src/Ratchet/WebSocket/WsServer.php index 61d42b6..0bcb50a 100644 --- a/src/Ratchet/WebSocket/WsServer.php +++ b/src/Ratchet/WebSocket/WsServer.php @@ -106,7 +106,7 @@ class WsServer implements HttpServerInterface { $streamer = new MessageBuffer( $this->closeFrameChecker, function(MessageInterface $msg) use ($wsConn) { - $this->delegate->onMessage($wsConn, $msg); + $this->delegate->onMessage($wsConn, $msg->getPayload(), $msg->isBinary()); }, function(FrameInterface $frame) use ($wsConn) { $this->onControlFrame($frame, $wsConn); diff --git a/tests/autobahn/bin/fuzzingserver.php b/tests/autobahn/bin/fuzzingserver.php index 093a5cf..4b0fb84 100644 --- a/tests/autobahn/bin/fuzzingserver.php +++ b/tests/autobahn/bin/fuzzingserver.php @@ -2,12 +2,18 @@ require dirname(dirname(dirname(__DIR__))) . '/vendor/autoload.php'; +class BinaryEcho extends \Ratchet\Server\EchoServer implements \Ratchet\WebSocket\BinaryMessageInterface { + public function onMessage(\Ratchet\ConnectionInterface $from, $msg, $isBinary = false) { + $from->send($msg, $isBinary); + } +} + $port = $argc > 1 ? $argv[1] : 8000; $impl = sprintf('React\EventLoop\%sLoop', $argc > 2 ? $argv[2] : 'StreamSelect'); $loop = new $impl; $sock = new React\Socket\Server($loop); - $app = new Ratchet\Http\HttpServer(new Ratchet\WebSocket\WsServer(new Ratchet\Server\EchoServer)); + $app = new Ratchet\Http\HttpServer(new Ratchet\WebSocket\WsServer(new BinaryEcho)); $sock->listen($port, '0.0.0.0'); From e3aecdf02108821e3de447fce479d80a5fbf0c95 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Tue, 1 Mar 2016 14:11:15 -0500 Subject: [PATCH 5/6] Alternative approach to binary messaging A new interface the dev can implement that will pass a Message object to the devs instance. The object has properties regarding binary/text --- .../WebSocket/BinaryMessageInterface.php | 8 ----- .../WebSocket/MessageCallableInterface.php | 8 +++++ .../WebSocket/MessageComponentInterface.php | 6 ++++ src/Ratchet/WebSocket/WsServer.php | 31 +++++++++++++++---- tests/autobahn/bin/fuzzingserver.php | 16 ++++++++-- 5 files changed, 52 insertions(+), 17 deletions(-) delete mode 100644 src/Ratchet/WebSocket/BinaryMessageInterface.php create mode 100644 src/Ratchet/WebSocket/MessageCallableInterface.php create mode 100644 src/Ratchet/WebSocket/MessageComponentInterface.php diff --git a/src/Ratchet/WebSocket/BinaryMessageInterface.php b/src/Ratchet/WebSocket/BinaryMessageInterface.php deleted file mode 100644 index e5dc022..0000000 --- a/src/Ratchet/WebSocket/BinaryMessageInterface.php +++ /dev/null @@ -1,8 +0,0 @@ -msgCb = function(ConnectionInterface $conn, MessageInterface $msg) { + $this->delegate->onMessage($conn, $msg); + }; + } elseif ($component instanceof DataComponentInterface) { + $this->msgCb = function(ConnectionInterface $conn, MessageInterface $msg) { + $this->delegate->onMessage($conn, $msg->getPayload()); + }; + } else { + throw new \UnexpectedValueException('Expected instance of \Ratchet\WebSocket\MessageComponentInterface or \Ratchet\MessageComponentInterface'); + } + $this->delegate = $component; $this->connections = new \SplObjectStorage; @@ -106,7 +124,8 @@ class WsServer implements HttpServerInterface { $streamer = new MessageBuffer( $this->closeFrameChecker, function(MessageInterface $msg) use ($wsConn) { - $this->delegate->onMessage($wsConn, $msg->getPayload(), $msg->isBinary()); + $cb = $this->msgCb; + $cb($wsConn, $msg); }, function(FrameInterface $frame) use ($wsConn) { $this->onControlFrame($frame, $wsConn); diff --git a/tests/autobahn/bin/fuzzingserver.php b/tests/autobahn/bin/fuzzingserver.php index 4b0fb84..5dcbf66 100644 --- a/tests/autobahn/bin/fuzzingserver.php +++ b/tests/autobahn/bin/fuzzingserver.php @@ -1,10 +1,20 @@ send($msg, $isBinary); +class BinaryEcho implements \Ratchet\WebSocket\MessageComponentInterface { + public function onMessage(ConnectionInterface $from, \Ratchet\RFC6455\Messaging\MessageInterface $msg) { + $from->send($msg); + } + + public function onOpen(ConnectionInterface $conn) { + } + + public function onClose(ConnectionInterface $conn) { + } + + public function onError(ConnectionInterface $conn, \Exception $e) { } } From 237615890d6eb84abc14cc8182c0ccd66fd02b76 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Tue, 1 Mar 2016 14:16:35 -0500 Subject: [PATCH 6/6] Remove edited API for sending binary (should pass message object instead) --- src/Ratchet/WebSocket/WsConnection.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Ratchet/WebSocket/WsConnection.php b/src/Ratchet/WebSocket/WsConnection.php index 3c34c9f..d2d04ef 100644 --- a/src/Ratchet/WebSocket/WsConnection.php +++ b/src/Ratchet/WebSocket/WsConnection.php @@ -12,10 +12,10 @@ class WsConnection extends AbstractConnectionDecorator { /** * {@inheritdoc} */ - public function send($msg, $isBinary = false) { + public function send($msg) { if (!$this->WebSocket->closing) { if (!($msg instanceof DataInterface)) { - $msg = new Frame($msg, true, ((boolean)$isBinary ? Frame::OP_BINARY : Frame::OP_TEXT)); + $msg = new Frame($msg); } $this->getConnection()->send($msg->getContents());