diff --git a/src/Ratchet/ConnectionInterface.php b/src/Ratchet/ConnectionInterface.php index 8d8098c..8a22472 100644 --- a/src/Ratchet/ConnectionInterface.php +++ b/src/Ratchet/ConnectionInterface.php @@ -1,7 +1,7 @@ send(json_encode(array(WAMP::MSG_EVENT, $uri, $msg))); + public function event($topic, $msg) { + $this->send(json_encode(array(WAMP::MSG_EVENT, $topic, $msg))); } /** @@ -67,7 +67,7 @@ class WampConnection extends AbstractConnectionDecorator { * @return string */ public function getUri($uri) { - return (isset($this->WAMP->prefixes[$uri]) ? $this->WAMP->prefixes[$uri] : $uri); + return (array_key_exists($uri, $this->WAMP->prefixes) ? $this->WAMP->prefixes[$uri] : $uri); } /** diff --git a/src/Ratchet/Wamp/WampServer.php b/src/Ratchet/Wamp/WampServer.php index 72d3739..6b6b8be 100644 --- a/src/Ratchet/Wamp/WampServer.php +++ b/src/Ratchet/Wamp/WampServer.php @@ -116,7 +116,15 @@ class WampServer implements MessageComponentInterface, WsServerInterface { case static::MSG_PUBLISH: $exclude = (array_key_exists(3, $json) ? $json[3] : null); - $eligible = (array_key_exists(4, $json) ? $json[4] : null); + if (!is_array($exclude)) { + if (true === (boolean)$exclude) { + $exclude = array($from->WAMP->sessionId); + } else { + $exclude = array(); + } + } + + $eligible = (array_key_exists(4, $json) ? $json[4] : array()); $this->_decorating->onPublish($from, $from->getUri($json[1]), $json[2], $exclude, $eligible); break; diff --git a/src/Ratchet/Wamp/WampServerInterface.php b/src/Ratchet/Wamp/WampServerInterface.php index 3bb4476..7dd2664 100644 --- a/src/Ratchet/Wamp/WampServerInterface.php +++ b/src/Ratchet/Wamp/WampServerInterface.php @@ -10,32 +10,34 @@ use Ratchet\ConnectionInterface; interface WampServerInterface extends ComponentInterface { /** * An RPC call has been received - * @param Ratchet\Connection - * @param string - * @param ... + * @param Ratchet\ConnectionInterface + * @param string The unique ID of the RPC, required to respond to + * @param string The topic to execute the call against * @param array Call parameters received from the client */ - function onCall(ConnectionInterface $conn, $id, $procURI, array $params); + function onCall(ConnectionInterface $conn, $id, $topic, array $params); /** - * A request to subscribe to a URI has been made - * @param Ratchet\Connection - * @param ... + * A request to subscribe to a topic has been made + * @param Ratchet\ConnectionInterface + * @param string The topic to subscribe to */ - function onSubscribe(ConnectionInterface $conn, $uri); + function onSubscribe(ConnectionInterface $conn, $topic); /** - * A request to unsubscribe from a URI has been made - * @param Ratchet\Connection - * @param ... + * A request to unsubscribe from a topic has been made + * @param Ratchet\ConnectionInterface + * @param The topic to unsubscribe from */ - function onUnSubscribe(ConnectionInterface $conn, $uri); + function onUnSubscribe(ConnectionInterface $conn, $topic); /** * A client is attempting to publish content to a subscribed connections on a URI - * @param Ratchet\Connection - * @param ... - * @param string + * @param Ratchet\ConnectionInterface + * @param The topic the user has attempted to publish to + * @param string Payload of the publish + * @param array A list of session IDs the message should be excluded from (blacklist) + * @param array A list of session Ids the message should be send to (whitelist) */ - function onPublish(ConnectionInterface $conn, $uri, $event, $exclude, $eligible); + function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude = array(), array $eligible = array()); } \ No newline at end of file diff --git a/tests/Ratchet/Tests/Mock/WampComponent.php b/tests/Ratchet/Tests/Mock/WampComponent.php index 803e999..2baf2a9 100644 --- a/tests/Ratchet/Tests/Mock/WampComponent.php +++ b/tests/Ratchet/Tests/Mock/WampComponent.php @@ -17,15 +17,15 @@ class WampComponent implements WampServerInterface, WsServerInterface { $this->last[__FUNCTION__] = func_get_args(); } - public function onSubscribe(ConnectionInterface $conn, $uri) { + public function onSubscribe(ConnectionInterface $conn, $topic) { $this->last[__FUNCTION__] = func_get_args(); } - public function onUnSubscribe(ConnectionInterface $conn, $uri) { + public function onUnSubscribe(ConnectionInterface $conn, $topic) { $this->last[__FUNCTION__] = func_get_args(); } - public function onPublish(ConnectionInterface $conn, $uri, $event, $exclude, $eligible) { + public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude = array(), array $eligible = array()) { $this->last[__FUNCTION__] = func_get_args(); } diff --git a/tests/Ratchet/Tests/Wamp/WampServerTest.php b/tests/Ratchet/Tests/Wamp/WampServerTest.php index 4643d9d..ae46143 100644 --- a/tests/Ratchet/Tests/Wamp/WampServerTest.php +++ b/tests/Ratchet/Tests/Wamp/WampServerTest.php @@ -118,16 +118,43 @@ class WampServerTest extends \PHPUnit_Framework_TestCase { $this->assertEquals($paramNum, count($this->_app->last['onCall'][3])); } - public function publishProvider() { - return array( - ); + public function testPublish() { + $conn = $this->newConn(); + + $topic = 'pubsubhubbub'; + $event = 'Here I am, publishing data'; + + $clientMessage = array(7, $topic, $event); + + $this->_comp->onOpen($conn); + $this->_comp->onMessage($conn, json_encode($clientMessage)); + + $this->assertEquals($topic, $this->_app->last['onPublish'][1]); + $this->assertEquals($event, $this->_app->last['onPublish'][2]); + $this->assertEquals(array(), $this->_app->last['onPublish'][3]); + $this->assertEquals(array(), $this->_app->last['onPublish'][4]); } - /** - * @dataProvider publishProvider - */ - public function TODOtestPublish() { - + public function testPublishAndExcludeMe() { + $conn = $this->newConn(); + + $this->_comp->onOpen($conn); + $this->_comp->onMessage($conn, json_encode(array(7, 'topic', 'event', true))); + + $this->assertEquals($conn->WAMP->sessionId, $this->_app->last['onPublish'][3][0]); + } + + public function testPublishAndEligible() { + $conn = $this->newConn(); + + $buddy = uniqid(); + $friend = uniqid(); + + $this->_comp->onOpen($conn); + $this->_comp->onMessage($conn, json_encode(array(7, 'topic', 'event', false, array($buddy, $friend)))); + + $this->assertEquals(array(), $this->_app->last['onPublish'][3]); + $this->assertEquals(2, count($this->_app->last['onPublish'][4])); } public function eventProvider() {