diff --git a/README.md b/README.md index 9c41811..3a47de9 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,26 @@ $emitter = new SocketIO\Emitter($redis); $emitter->emit('event', 'payload str'); ``` +### Namespacing keys +You can set a base namespace on initialisation: +```php +$redis = new \Redis(); +$redis->connect('127.0.0.1', '6379'); +$emitter = new SocketIO\Emitter($redis, array( + 'key' => 'socket-io:' +)); +``` + +You can then specify events on specific emits: +```php +$emitter->emit('my-event', 'payload str'); +``` +Which will show up as: + +```sh +"PUBLISH" "socket-io:my-event" ... +``` + ### Broadcasting and other flags Possible flags * json diff --git a/src/Emitter.php b/src/Emitter.php index 7df0255..c915915 100644 --- a/src/Emitter.php +++ b/src/Emitter.php @@ -2,140 +2,196 @@ namespace SocketIO; -define('EVENT', 2); -define('BINARY_EVENT', 5); - if (!function_exists('msgpack_pack')) { require(__DIR__ . '/msgpack_pack.php'); } -class Emitter { - public function __construct($redis = FALSE, $opts = array()) { - if (is_array($redis)) { - $opts = $redis; - $redis = FALSE; - } - - // Apply default arguments - $opts = array_merge(array('host' => 'localhost', 'port' => 6379), $opts); - - if (!$redis) { - // Default to phpredis - if (extension_loaded('redis')) { - if (!isset($opts['socket']) && !isset($opts['host'])) throw new \Exception('Host should be provided when not providing a redis instance'); - if (!isset($opts['socket']) && !isset($opts['port'])) throw new \Exception('Port should be provided when not providing a redis instance'); +class Emitter +{ + /** + * Event + * @var int + */ + public $event = 2; + + /** + * Binary event + * @var int + */ + public $binaryEvent = 5; + + /** + * Redis + * @var \Redis + */ + protected $redis; + + /** + * Event key + * @var string + */ + protected $key; + + /** + * Rooms + * @var array + */ + protected $_rooms = array(); + + /** + * Flags + * @var array + */ + protected $_flags = array(); + + + public function __construct($redis = false, $opts = array()) { + + if (is_array($redis)) { + $opts = $redis; + $redis = false; + } - $redis = new \Redis(); - if (isset($opts['socket'])) { - $redis->connect($opts['socket']); - } else { - $redis->connect($opts['host'], $opts['port']); + // Apply default arguments + $opts = array_merge(array('host' => 'localhost', 'port' => 6379), $opts); + + if ($redis == false) { + // Default to phpredis + if (extension_loaded('redis')) { + if (!isset($opts['socket']) && !isset($opts['host'])) throw new \Exception('Host should be provided when not providing a redis instance'); + if (!isset($opts['socket']) && !isset($opts['port'])) throw new \Exception('Port should be provided when not providing a redis instance'); + + $redis = new \Redis(); + if (isset($opts['socket'])) { + $redis->connect($opts['socket']); + } else { + $redis->connect($opts['host'], $opts['port']); + } + } else { + $redis = new \TinyRedisClient($opts['host'].':'.$opts['port']); + } } - } else { - $redis = new \TinyRedisClient($opts['host'].':'.$opts['port']); - } - } - if (!is_callable(array($redis, 'publish'))) { - throw new \Exception('The Redis client provided is invalid. The client needs to implement the publish method. Try using the default client.'); - } + // Fail on unsupported redis + if (!is_callable(array($redis, 'publish'))) { + throw new \Exception('The Redis client provided is invalid. The client needs to implement the publish method. Try using the default client.'); + } - $this->redis = $redis; - $this->key = (isset($opts['key']) ? $opts['key'] : 'socket.io') . '#emitter'; + $this->redis = $redis; + $this->key = (isset($opts['key']) ? $opts['key'] : 'socket.io'); - $this->_rooms = array(); - $this->_flags = array(); - } + $this->_rooms = array(); + $this->_flags = array(); + } - /* - * Flags - */ + /** + * Flags + * + * @param string $flag + * @return SocketIO\Emitter + */ + public function __get($flag) { + $this->_flags[$flag] = TRUE; + return $this; + } - public function __get($flag) { - $this->_flags[$flag] = TRUE; - return $this; - } + /** + * Read flags + * + * @param string $flag + * @return bool + */ + private function readFlag($flag) { + return isset($this->_flags[$flag]) ? $this->_flags[$flag] : false; + } - private function readFlag($flag) { - return isset($this->_flags[$flag]) ? $this->_flags[$flag] : false; - } + /** + * Broadcasting + * + * @param string $room + * @return SocketIO\Emitter + */ + public function in($room) { + if (!in_array($room, $this->_rooms)) { + $this->_rooms[] = $room; + } - /* - * Broadcasting - */ + return $this; + } - public function in($room) { - if (!in_array($room, $this->_rooms)) { - $this->_rooms[] = $room; + /** + * Alias for $this->in() + * + * @param string $room + * @return SocketIO\Emitter + */ + public function to($room) { + return $this->in($room); } - return $this; - } - - // Alias for in - public function to($room) { - return $this->in($room); - } - - /* - * Namespace - */ - - public function of($nsp) { - $this->_flags['nsp'] = $nsp; - return $this; - } - - /* - * Emitting - */ - - public function emit() { - $args = func_get_args(); - $packet = array(); - - $packet['type'] = EVENT; - // handle binary wrapper args - for ($i = 0; $i < count($args); $i++) { - $arg = $args[$i]; - if ($arg instanceof Binary) { - $args[$i] = strval($arg); - $this->binary; - } + /** + * Namespaces + * + * @param string $nsp + * @return SocketIO\Emitter + */ + public function of($nsp) { + $this->_flags['nsp'] = $nsp; + return $this; } - if ($this->readFlag('binary')) $packet['type'] = BINARY_EVENT; + /** + * Emit the data + * + * @return SocketIO\Emitter + */ + public function emit($key = '', $data) { + + $packet = array(); + $packet['type'] = $this->event; + + // handle binary wrapper args + for ($i = 0; $i < count($data); $i++) { + $arg = $data[$i]; + if ($arg instanceof Binary) { + $data[$i] = strval($arg); + } + } - $packet['data'] = $args; + // If catch + if ($this->readFlag('binary')) $packet['type'] = $this->binaryEvent; - // set namespace - if (isset($this->_flags['nsp'])) { - $packet['nsp'] = $this->_flags['nsp']; - unset($this->_flags['nsp']); - } else { - $packet['nsp'] = '/'; - } + $packet['data'] = $data; - // publish - $packed = msgpack_pack(array($packet, array( - 'rooms' => $this->_rooms, - 'flags' => $this->_flags - ))); + // set namespaces + if (isset($this->_flags['nsp'])) { + $packet['nsp'] = $this->_flags['nsp']; + unset($this->_flags['nsp']); + } else { + $packet['nsp'] = '/'; + } - // hack buffer extensions for msgpack with binary - if ($packet['type'] == BINARY_EVENT) { - $packed = str_replace(pack('c', 0xda), pack('c', 0xd8), $packed); - $packed = str_replace(pack('c', 0xdb), pack('c', 0xd9), $packed); - } + // publish + $packed = msgpack_pack( array($packet, array( + 'rooms' => $this->_rooms, + 'flags' => $this->_flags + )) + ); + + // hack buffer extensions for msgpack with binary + if ($packet['type'] == $this->binaryEvent) { + $packed = str_replace(pack('c', 0xda), pack('c', 0xd8), $packed); + $packed = str_replace(pack('c', 0xdb), pack('c', 0xd9), $packed); + } - $this->redis->publish($this->key, $packed); + $this->redis->publish($this->key . $key, $packed); - // reset state - $this->_rooms = array(); - $this->_flags = array(); + // reset state + $this->_rooms = array(); + $this->_flags = array(); - return $this; - } + return $this; + } }