vendor/ashiina/socket.io-emitter/src/Emitter.php line 48

Open in your IDE?
  1. <?php
  2. namespace SocketIO;
  3. define('EVENT'2);
  4. define('BINARY_EVENT'5);
  5. if (!function_exists('msgpack_pack')) {
  6.   require(__DIR__ '/msgpack_pack.php');
  7. }
  8. class Emitter {
  9.   private $uid 'emitter';
  10.   public function __construct($redis FALSE$opts = array()) {
  11.     if (is_array($redis)) {
  12.       $opts $redis;
  13.       $redis FALSE;
  14.     }
  15.     // Apply default arguments
  16.     $opts array_merge(array('host' => 'localhost''port' => 6379), $opts);
  17.     if (!$redis) {
  18.       // Default to phpredis
  19.       if (extension_loaded('redis')) {
  20.         if (!isset($opts['socket']) && !isset($opts['host'])) throw new \Exception('Host should be provided when not providing a redis instance');
  21.         if (!isset($opts['socket']) && !isset($opts['port'])) throw new \Exception('Port should be provided when not providing a redis instance');
  22.         $redis = new \Redis();
  23.         if (isset($opts['socket'])) {
  24.           $redis->connect($opts['socket']);
  25.         } else {
  26.           $redis->connect($opts['host'], $opts['port']);
  27.         }
  28.       } else {
  29.         $redis = new \TinyRedisClient($opts['host'].':'.$opts['port']);
  30.       }
  31.     }
  32.     if (!is_callable(array($redis'publish'))) {
  33.       throw new \Exception('The Redis client provided is invalid. The client needs to implement the publish method. Try using the default client.');
  34.     }
  35.     $this->redis $redis;
  36.     $this->prefix = isset($opts['key']) ? $opts['key'] : 'socket.io';
  37.     $this->_rooms = array();
  38.     $this->_flags = array();
  39.   }
  40.   /*
  41.    * Flags
  42.    */
  43.   public function __get($flag) {
  44.     $this->_flags[$flag] = TRUE;
  45.     return $this;
  46.   }
  47.   private function readFlag($flag) {
  48.     return isset($this->_flags[$flag]) ? $this->_flags[$flag] : false;
  49.   }
  50.   /*
  51.    * Broadcasting
  52.    */
  53.   public function in($room) {
  54.     if (!in_array($room$this->_rooms)) {
  55.       $this->_rooms[] = $room;
  56.     }
  57.     return $this;
  58.   }
  59.   // Alias for in
  60.   public function to($room) {
  61.     return $this->in($room);
  62.   }
  63.   /*
  64.    * Namespace
  65.    */
  66.   public function of($nsp) {
  67.     $this->_flags['nsp'] = $nsp;
  68.     return $this;
  69.   }
  70.   /*
  71.    * Emitting
  72.    */
  73.   public function emit() {
  74.     $args func_get_args();
  75.     $packet = array();
  76.     $packet['type'] = EVENT;
  77.     // handle binary wrapper args
  78.     for ($i 0$i count($args); $i++) {
  79.       $arg $args[$i];
  80.       if ($arg instanceof Binary) {
  81.         $args[$i] = strval($arg);
  82.         $this->binary;
  83.       }
  84.     }
  85.     if ($this->readFlag('binary')) $packet['type'] = BINARY_EVENT;
  86.     $packet['data'] = $args;
  87.     // set namespace
  88.     if (isset($this->_flags['nsp'])) {
  89.       $packet['nsp'] = $this->_flags['nsp'];
  90.       unset($this->_flags['nsp']);
  91.     } else {
  92.       $packet['nsp'] = '/';
  93.     }
  94.     $opts = array(
  95.       'rooms' => $this->_rooms,
  96.       'flags' => $this->_flags
  97.     );
  98.     $chn $this->prefix '#' $packet['nsp'] . '#';
  99.     $packed msgpack_pack(array($this->uid,$packet,$opts));
  100.     // hack buffer extensions for msgpack with binary
  101.     if ($packet['type'] == BINARY_EVENT) {
  102.       $packed str_replace(pack('c'0xda), pack('c'0xd8), $packed);
  103.       $packed str_replace(pack('c'0xdb), pack('c'0xd9), $packed);
  104.     }
  105.     // publish
  106.     if (is_array($this->_rooms) && count($this->_rooms) > 0) {
  107.         foreach ($this->_rooms as $room) {
  108.             $chnRoom $chn $room '#';
  109.             $this->redis->publish($chnRoom$packed);
  110.         }
  111.     } else {
  112.         $this->redis->publish($chn$packed);
  113.     }
  114.     // reset state
  115.     $this->_rooms = array();
  116.     $this->_flags = array();
  117.     return $this;
  118.   }
  119. }