diff --git a/src/Emitter.php b/src/Emitter.php index 7df0255..3298b45 100644 --- a/src/Emitter.php +++ b/src/Emitter.php @@ -10,6 +10,8 @@ } class Emitter { + private $uid = 'emitter'; + public function __construct($redis = FALSE, $opts = array()) { if (is_array($redis)) { $opts = $redis; @@ -41,7 +43,7 @@ public function __construct($redis = FALSE, $opts = array()) { } $this->redis = $redis; - $this->key = (isset($opts['key']) ? $opts['key'] : 'socket.io') . '#emitter'; + $this->prefix = isset($opts['key']) ? $opts['key'] : 'socket.io'; $this->_rooms = array(); $this->_flags = array(); @@ -116,11 +118,12 @@ public function emit() { $packet['nsp'] = '/'; } - // publish - $packed = msgpack_pack(array($packet, array( + $opts = array( 'rooms' => $this->_rooms, 'flags' => $this->_flags - ))); + ); + $chn = $this->prefix . '#' . $packet['nsp'] . '#'; + $packed = msgpack_pack(array($this->uid,$packet,$opts)); // hack buffer extensions for msgpack with binary if ($packet['type'] == BINARY_EVENT) { @@ -128,7 +131,15 @@ public function emit() { $packed = str_replace(pack('c', 0xdb), pack('c', 0xd9), $packed); } - $this->redis->publish($this->key, $packed); + // publish + if (is_array($this->_rooms) && count($this->_rooms) > 0) { + foreach ($this->_rooms as $room) { + $chnRoom = $chn . $room . '#'; + $this->redis->publish($chnRoom, $packed); + } + } else { + $this->redis->publish($chn, $packed); + } // reset state $this->_rooms = array(); diff --git a/test/test.php b/test/test.php index 0fe65ec..5d8c687 100644 --- a/test/test.php +++ b/test/test.php @@ -141,5 +141,50 @@ public function testPublishContainsNamespaceWhenEmittingWithNamespaceSet() { $this->assertTrue(strpos($contents, '/nsp') !== FALSE); } + + public function testPublishKeyNameWithNamespaceSet() { + $p = new Process('redis-cli monitor > redis.log'); + + sleep(1); + // Running this should produce something that's visible in `redis-cli monitor` + $emitter = new Emitter(NULL, array('host' => '127.0.0.1', 'port' => '6379')); + $emitter->of('/nsp')->emit('yolo', 'data'); + + $p->stop(); + $contents= file_get_contents('redis.log'); + unlink('redis.log'); + + $this->assertTrue(strpos($contents, 'socket.io#/nsp#') !== FALSE); + } + + public function testPublishKeyNameWithRoomSet() { + $p = new Process('redis-cli monitor > redis.log'); + + sleep(1); + // Running this should produce something that's visible in `redis-cli monitor` + $emitter = new Emitter(NULL, array('host' => '127.0.0.1', 'port' => '6379')); + $emitter->to('rm')->emit('yolo', 'data'); + + $p->stop(); + $contents= file_get_contents('redis.log'); + unlink('redis.log'); + + $this->assertTrue(strpos($contents, 'socket.io#/#rm#') !== FALSE); + } + + public function testPublishKeyNameWithNamespaceAndRoomSet() { + $p = new Process('redis-cli monitor > redis.log'); + + sleep(1); + // Running this should produce something that's visible in `redis-cli monitor` + $emitter = new Emitter(NULL, array('host' => '127.0.0.1', 'port' => '6379')); + $emitter->of('/nsp')->to('rm')->emit('yolo', 'data'); + + $p->stop(); + $contents= file_get_contents('redis.log'); + unlink('redis.log'); + + $this->assertTrue(strpos($contents, 'socket.io#/nsp#rm#') !== FALSE); + } } ?>