From 02ffd6f782507775ced63459a3abbb004614b896 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sun, 20 Nov 2011 14:46:15 -0500 Subject: [PATCH] Interface change Changed onRecv to onMessage in Interface to match a standard api Removed Frame::__toString methods - could imply different return expectation --- README.md | 2 +- lib/Ratchet/Application/ApplicationInterface.php | 2 +- lib/Ratchet/Application/Server/App.php | 6 +++--- lib/Ratchet/Application/WebSocket/App.php | 4 ++-- .../WebSocket/Version/FrameInterface.php | 5 ----- .../WebSocket/Version/Hixie76/Frame.php | 4 ---- .../Application/WebSocket/Version/HyBi10/Frame.php | 4 ---- .../WebSocket/Version/HyBi10/Message.php | 2 +- lib/Ratchet/ObserverInterface.php | 2 +- .../Application/WebSocket/Version/HyBi10Test.php | 14 ++++++++++---- tests/Ratchet/Tests/Mock/Application.php | 2 +- 11 files changed, 20 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index cd03583..435d819 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ class Chat implements ApplicationInterface { $this->_clients->attach($conn); } - public function onRecv(Connection $from, $msg) { + public function onMessage(Connection $from, $msg) { $commands = new Cmds; foreach ($this->_clients as $client) { diff --git a/lib/Ratchet/Application/ApplicationInterface.php b/lib/Ratchet/Application/ApplicationInterface.php index a47306b..0666995 100644 --- a/lib/Ratchet/Application/ApplicationInterface.php +++ b/lib/Ratchet/Application/ApplicationInterface.php @@ -23,7 +23,7 @@ interface ApplicationInterface { * @param string The message received * @return Ratchet\Resource\Command\CommandInterface|null */ - function onRecv(Connection $from, $msg); + function onMessage(Connection $from, $msg); /** * This is called before or after a socket is closed (depends on how it's closed). SendMessage to $conn will not result in an error if it has already been closed. diff --git a/lib/Ratchet/Application/Server/App.php b/lib/Ratchet/Application/Server/App.php index 179cefa..831f425 100644 --- a/lib/Ratchet/Application/Server/App.php +++ b/lib/Ratchet/Application/Server/App.php @@ -110,7 +110,7 @@ class App implements ApplicationInterface { } */ - $res = $this->onRecv($conn, $data); + $res = $this->onMessage($conn, $data); } else { $res = $this->onClose($conn); } @@ -144,8 +144,8 @@ class App implements ApplicationInterface { return $this->_app->onOpen($new_connection); } - public function onRecv(Connection $from, $msg) { - return $this->_app->onRecv($from, $msg); + public function onMessage(Connection $from, $msg) { + return $this->_app->onMessage($from, $msg); } public function onClose(Connection $conn) { diff --git a/lib/Ratchet/Application/WebSocket/App.php b/lib/Ratchet/Application/WebSocket/App.php index b398d8d..29eb303 100644 --- a/lib/Ratchet/Application/WebSocket/App.php +++ b/lib/Ratchet/Application/WebSocket/App.php @@ -66,7 +66,7 @@ class App implements ApplicationInterface, ConfiguratorInterface { $conn->WebSocket->headers = ''; } - public function onRecv(Connection $from, $msg) { + public function onMessage(Connection $from, $msg) { if (true !== $from->WebSocket->handshake) { if (!isset($from->WebSocket->version)) { try { @@ -125,7 +125,7 @@ class App implements ApplicationInterface, ConfiguratorInterface { } if ($from->WebSocket->message->isCoalesced()) { - $cmds = $this->prepareCommand($this->_app->onRecv($from, (string)$from->WebSocket->message)); + $cmds = $this->prepareCommand($this->_app->onMessage($from, (string)$from->WebSocket->message)); unset($from->WebSocket->message); return $cmds; diff --git a/lib/Ratchet/Application/WebSocket/Version/FrameInterface.php b/lib/Ratchet/Application/WebSocket/Version/FrameInterface.php index 3056094..0776b6c 100644 --- a/lib/Ratchet/Application/WebSocket/Version/FrameInterface.php +++ b/lib/Ratchet/Application/WebSocket/Version/FrameInterface.php @@ -2,11 +2,6 @@ namespace Ratchet\Application\WebSocket\Version; interface FrameInterface { - /** - * @alias getPayload - */ - function __toString(); - /** * Dunno if I'll use this * Thinking could be used if a control frame? diff --git a/lib/Ratchet/Application/WebSocket/Version/Hixie76/Frame.php b/lib/Ratchet/Application/WebSocket/Version/Hixie76/Frame.php index 4fcdec8..26adf02 100644 --- a/lib/Ratchet/Application/WebSocket/Version/Hixie76/Frame.php +++ b/lib/Ratchet/Application/WebSocket/Version/Hixie76/Frame.php @@ -12,10 +12,6 @@ class Frame implements FrameInterface { */ protected $_data = ''; - public function __toString() { - return $this->getPayload(); - } - public function isCoalesced() { return (boolean)($this->_data[0] == chr(0) && substr($this->_data, -1) == chr(255)); } diff --git a/lib/Ratchet/Application/WebSocket/Version/HyBi10/Frame.php b/lib/Ratchet/Application/WebSocket/Version/HyBi10/Frame.php index 5223a96..0ee94a5 100644 --- a/lib/Ratchet/Application/WebSocket/Version/HyBi10/Frame.php +++ b/lib/Ratchet/Application/WebSocket/Version/HyBi10/Frame.php @@ -27,10 +27,6 @@ class Frame implements FrameInterface { */ protected $_pay_check = -1; - public function __toString() { - return (string)$this->getPayload(); - } - public function isCoalesced() { try { $payload_length = $this->getPayloadLength(); diff --git a/lib/Ratchet/Application/WebSocket/Version/HyBi10/Message.php b/lib/Ratchet/Application/WebSocket/Version/HyBi10/Message.php index ccca59f..add834c 100644 --- a/lib/Ratchet/Application/WebSocket/Version/HyBi10/Message.php +++ b/lib/Ratchet/Application/WebSocket/Version/HyBi10/Message.php @@ -55,7 +55,7 @@ class Message implements MessageInterface { $buffer = ''; foreach ($this->_frames as $frame) { - $buffer .= (string)$frame; + $buffer .= $frame->getPayload(); } return $buffer; diff --git a/lib/Ratchet/ObserverInterface.php b/lib/Ratchet/ObserverInterface.php index 7fa54f2..ca87e95 100644 --- a/lib/Ratchet/ObserverInterface.php +++ b/lib/Ratchet/ObserverInterface.php @@ -21,7 +21,7 @@ interface ObserverInterface { * @param string The message received * @return Ratchet\Resource\Command\CommandInterface|null */ - function onRecv(SocketInterface $from, $msg); + function onMessage(SocketInterface $from, $msg); /** * This is called before or after a socket is closed (depends on how it's closed). SendMessage to $conn will not result in an error if it has already been closed. diff --git a/tests/Ratchet/Tests/Application/WebSocket/Version/HyBi10Test.php b/tests/Ratchet/Tests/Application/WebSocket/Version/HyBi10Test.php index a78dc26..6585da3 100644 --- a/tests/Ratchet/Tests/Application/WebSocket/Version/HyBi10Test.php +++ b/tests/Ratchet/Tests/Application/WebSocket/Version/HyBi10Test.php @@ -1,6 +1,7 @@ _version->unframe(base64_decode($framed)); - $this->assertEquals($message, $decoded['payload']); +// $decoded = $this->_version->unframe(base64_decode($framed)); + $frame = new Frame; + $frame->addBuffer(base64_decode($framed)); + + $this->assertEquals($message, $frame->getPayload()); } public static function UnframeMessageProvider() { @@ -54,8 +58,10 @@ class HyBi10Test extends \PHPUnit_Framework_TestCase { public function testUnframeMatchesPreFraming() { $string = 'Hello World!'; $framed = $this->_version->frame($string); - $unframed = $this->_version->unframe($framed); - $this->assertEquals($string, $unframed['payload']); + $frame = new Frame; + $frame->addBuffer($framed); + + $this->assertEquals($string, $frame->getPayload()); } } \ No newline at end of file diff --git a/tests/Ratchet/Tests/Mock/Application.php b/tests/Ratchet/Tests/Mock/Application.php index 0b76dc6..ac3314b 100644 --- a/tests/Ratchet/Tests/Mock/Application.php +++ b/tests/Ratchet/Tests/Mock/Application.php @@ -26,7 +26,7 @@ class Application implements ApplicationInterface { $this->_conn_open = $conn; } - public function onRecv(Connection $from, $msg) { + public function onMessage(Connection $from, $msg) { $this->_conn_recv = $from; $this->_msg_recv = $msg; }