[WAMP] [BCB] Publishing

BC break: Updated the `WampServerInterface` to have a strict API
Exclude and Eligible are now always arrays acting like black/white list
Changed `uri` to `topic` to be more generic with Pub/Sub
Added unit tests for `onPublish`
This commit is contained in:
Chris Boden 2012-05-19 13:30:58 -04:00
parent 598da1e8b0
commit 986edd9562
6 changed files with 72 additions and 35 deletions

View File

@ -1,7 +1,7 @@
<?php
namespace Ratchet;
const VERSION = 'Ratchet/0.1';
const VERSION = 'Ratchet/0.1.2';
/**
* A proxy object representing a connection to the application

View File

@ -34,8 +34,8 @@ class WampConnection extends AbstractConnectionDecorator {
* @param string A developer-oriented description of the error
* @param string|null An optional human readable detail message to send back
*/
public function callError($id, $uri, $desc = '', $details = null) {
$data = array(WAMP::MSG_CALL_ERROR, $id, $uri, $desc);
public function callError($id, $topic, $desc = '', $details = null) {
$data = array(WAMP::MSG_CALL_ERROR, $id, $topic, $desc);
if (null !== $details) {
$data[] = $details;
@ -45,11 +45,11 @@ class WampConnection extends AbstractConnectionDecorator {
}
/**
* @param string The URI or CURIE to broadcast to
* @param string The topic to broadcast to
* @param mixed Data to send with the event. Anything that is json'able
*/
public function event($uri, $msg) {
$this->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);
}
/**

View File

@ -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;

View File

@ -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());
}

View File

@ -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();
}

View File

@ -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() {