Merge branch 'refs/heads/wamp-broadcasting'

This commit is contained in:
Chris Boden 2014-02-16 19:10:03 -05:00
commit 984cfbfbee
7 changed files with 83 additions and 15 deletions

View File

@ -30,12 +30,23 @@ class Topic implements \IteratorAggregate, \Countable {
} }
/** /**
* Send a message to all the connections in this topic * Send a message to all the connections in this topic
* @param string $msg * @param string $msg Payload to publish
* @return Topic * @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)
public function broadcast($msg) { * @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) { 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); $client->event($this->id, $msg);
} }
@ -85,4 +96,4 @@ class Topic implements \IteratorAggregate, \Countable {
public function count() { public function count() {
return $this->subscribers->count(); return $this->subscribers->count();
} }
} }

View File

@ -99,4 +99,5 @@ class WampConnection extends AbstractConnectionDecorator {
public function close($opt = null) { public function close($opt = null) {
$this->getConnection()->close($opt); $this->getConnection()->close($opt);
} }
}
}

View File

@ -1,7 +1,5 @@
<?php <?php
namespace Ratchet\Wamp; namespace Ratchet\Wamp;
use Ratchet\Wamp\ServerProtocol;
use Ratchet\Wamp\WampConnection;
use Ratchet\Mock\Connection; use Ratchet\Mock\Connection;
use Ratchet\Mock\WampComponent as TestComponent; use Ratchet\Mock\WampComponent as TestComponent;

View File

@ -1,6 +1,5 @@
<?php <?php
namespace Ratchet\Wamp; namespace Ratchet\Wamp;
use Ratchet\Wamp\TopicManager;
/** /**
* @covers Ratchet\Wamp\TopicManager * @covers Ratchet\Wamp\TopicManager

View File

@ -1,7 +1,5 @@
<?php <?php
namespace Ratchet\Wamp; namespace Ratchet\Wamp;
use Ratchet\Wamp\Topic;
use Ratchet\Wamp\WampConnection;
/** /**
* @covers Ratchet\Wamp\Topic * @covers Ratchet\Wamp\Topic
@ -60,6 +58,60 @@ class TopicTest extends \PHPUnit_Framework_TestCase {
$topic->broadcast($msg); $topic->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() { public function testIterator() {
$first = $this->newConn(); $first = $this->newConn();
$second = $this->newConn(); $second = $this->newConn();
@ -109,4 +161,4 @@ class TopicTest extends \PHPUnit_Framework_TestCase {
protected function newConn() { protected function newConn() {
return new WampConnection($this->getMock('\\Ratchet\\ConnectionInterface')); return new WampConnection($this->getMock('\\Ratchet\\ConnectionInterface'));
} }
} }

View File

@ -1,6 +1,5 @@
<?php <?php
namespace Ratchet\Wamp; namespace Ratchet\Wamp;
use Ratchet\Wamp\WampConnection;
/** /**
* @covers Ratchet\Wamp\WampConnection * @covers Ratchet\Wamp\WampConnection
@ -32,6 +31,15 @@ class WampConnectionTest extends \PHPUnit_Framework_TestCase {
$this->conn->callError($callId, $uri); $this->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() { public function testDetailedCallError() {
$callId = uniqid(); $callId = uniqid();
$uri = 'http://example.com/end/point'; $uri = 'http://example.com/end/point';

View File

@ -1,6 +1,5 @@
<?php <?php
namespace Ratchet\Wamp; namespace Ratchet\Wamp;
use Ratchet\Wamp\WampServer;
use Ratchet\AbstractMessageComponentTestCase; use Ratchet\AbstractMessageComponentTestCase;
/** /**