diff --git a/src/Ratchet/Wamp/Topic.php b/src/Ratchet/Wamp/Topic.php index 688195a..f1bd68a 100644 --- a/src/Ratchet/Wamp/Topic.php +++ b/src/Ratchet/Wamp/Topic.php @@ -30,12 +30,23 @@ class Topic implements \IteratorAggregate, \Countable { } /** - * Send a message to all the connections in this topic - * @param string $msg - * @return Topic - */ - public function broadcast($msg) { + * Send a message to all the connections in this topic + * @param string $msg Payload to publish + * @param array $exclude A list of session IDs the message should be excluded from (blacklist) + * @param array $eligible A list of session Ids the message should be send to (whitelist) + * @return Topic The same Topic object to chain + */ + public function broadcast($msg, array $exclude = array(), array $eligible = array()) { + $useEligible = (bool)count($eligible); foreach ($this->subscribers as $client) { + if (in_array($client->WAMP->sessionId, $exclude)) { + continue; + } + + if ($useEligible && !in_array($client->WAMP->sessionId, $eligible)) { + continue; + } + $client->event($this->id, $msg); } @@ -85,4 +96,4 @@ class Topic implements \IteratorAggregate, \Countable { public function count() { return $this->subscribers->count(); } -} \ No newline at end of file +} diff --git a/src/Ratchet/Wamp/WampConnection.php b/src/Ratchet/Wamp/WampConnection.php index de13844..95e1969 100644 --- a/src/Ratchet/Wamp/WampConnection.php +++ b/src/Ratchet/Wamp/WampConnection.php @@ -99,4 +99,5 @@ class WampConnection extends AbstractConnectionDecorator { public function close($opt = null) { $this->getConnection()->close($opt); } -} \ No newline at end of file + +} diff --git a/tests/unit/Wamp/ServerProtocolTest.php b/tests/unit/Wamp/ServerProtocolTest.php index b4f57a4..14a8b1e 100644 --- a/tests/unit/Wamp/ServerProtocolTest.php +++ b/tests/unit/Wamp/ServerProtocolTest.php @@ -1,7 +1,5 @@ broadcast($msg); } + public function testBroadcastWithExclude() { + $msg = 'Hello odd numbers'; + $name = 'Excluding'; + $protocol = json_encode(array(8, $name, $msg)); + + $first = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface'))); + $second = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface'))); + $third = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface'))); + + $first->expects($this->once()) + ->method('send') + ->with($this->equalTo($protocol)); + + $second->expects($this->never())->method('send'); + + $third->expects($this->once()) + ->method('send') + ->with($this->equalTo($protocol)); + + $topic = new Topic($name); + $topic->add($first); + $topic->add($second); + $topic->add($third); + + $topic->broadcast($msg, array($second->WAMP->sessionId)); + } + + public function testBroadcastWithEligible() { + $msg = 'Hello white list'; + $name = 'Eligible'; + $protocol = json_encode(array(8, $name, $msg)); + + $first = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface'))); + $second = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface'))); + $third = $this->getMock('Ratchet\\Wamp\\WampConnection', array('send'), array($this->getMock('\\Ratchet\\ConnectionInterface'))); + + $first->expects($this->once()) + ->method('send') + ->with($this->equalTo($protocol)); + + $second->expects($this->never())->method('send'); + + $third->expects($this->once()) + ->method('send') + ->with($this->equalTo($protocol)); + + $topic = new Topic($name); + $topic->add($first); + $topic->add($second); + $topic->add($third); + + $topic->broadcast($msg, array(), array($first->WAMP->sessionId, $third->WAMP->sessionId)); + } + public function testIterator() { $first = $this->newConn(); $second = $this->newConn(); @@ -109,4 +161,4 @@ class TopicTest extends \PHPUnit_Framework_TestCase { protected function newConn() { return new WampConnection($this->getMock('\\Ratchet\\ConnectionInterface')); } -} \ No newline at end of file +} diff --git a/tests/unit/Wamp/WampConnectionTest.php b/tests/unit/Wamp/WampConnectionTest.php index 7f82f98..e33c166 100644 --- a/tests/unit/Wamp/WampConnectionTest.php +++ b/tests/unit/Wamp/WampConnectionTest.php @@ -1,6 +1,5 @@ conn->callError($callId, $uri); } + public function testCallErrorWithTopic() { + $callId = uniqid(); + $uri = 'http://example.com/end/point'; + + $this->mock->expects($this->once())->method('send')->with(json_encode(array(4, $callId, $uri, ''))); + + $this->conn->callError($callId, new Topic($uri)); + } + public function testDetailedCallError() { $callId = uniqid(); $uri = 'http://example.com/end/point'; diff --git a/tests/unit/Wamp/WampServerTest.php b/tests/unit/Wamp/WampServerTest.php index e76d693..02c81a6 100644 --- a/tests/unit/Wamp/WampServerTest.php +++ b/tests/unit/Wamp/WampServerTest.php @@ -1,6 +1,5 @@