diff --git a/README.md b/README.md index 9c41811..9a8d50f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,12 @@ + +### *Maintained Fork* + +This is a fork of [rase-/socket.io-php-emitter](https://github.com/rase-/socket.io-php-emitter). +Since the original repo is not being maintained anymore and is having compatibility problems with +newer versions of Socket.io, I recommend using this repo, and also submitting issues here. + +[@ashiina](https://github.com/ashiina) + socket.io-php-emitter ===================== @@ -6,7 +15,7 @@ A PHP implementation of socket.io-emitter. This project requires a Redis client for PHP. If you dont have the [PECL Redis](https://github.com/nicolasff/phpredis) installed, the emitter will default to using [TinyRedisClient](https://github.com/ptrofimov/tinyredisclient). You can, however, pass in any Redis client that supports a `publish` method. ## Installation and development -To install and use in your PHP project, install it as a [composer package](https://packagist.org/packages/rase/socket.io-emitter). Install dependencies with `composer install`. +To install and use in your PHP project, install it as a [composer package](https://packagist.org/packages/ashiina/socket.io-emitter). Install dependencies with `composer install`. To run tests, invoke `make test`. The current test suite will just be checking redis monitor that everything is published correctly. Some work will be put into making a better integration test suite in the near future. diff --git a/composer.json b/composer.json index 3cef51a..8e848bb 100644 --- a/composer.json +++ b/composer.json @@ -1,10 +1,10 @@ { - "name": "rase/socket.io-emitter", - "description": "A PHP implementation of socket.io-emitter", - "homepage": "https://github.com/rase-/socket.io-php-emitter", + "name": "ashiina/socket.io-emitter", + "description": "*Maintained* A PHP implementation of socket.io-emitter", + "homepage": "https://github.com/ashiina/socket.io-php-emitter", "support": { - "issues": "https://github.com/rase-/socket.io-php-emitter/issues", - "source": "https://github.com/rase-/socket.io-php-emitter" + "issues": "https://github.com/ashiina/socket.io-php-emitter/issues", + "source": "https://github.com/ashiina/socket.io-php-emitter" }, "license": "MIT", "keywords": [ 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); + } } ?>