Nsp.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace PHPSocketIO;
  3. use PHPSocketIO\Event\Emitter;
  4. use PHPSocketIO\Parser\Parser;
  5. class Nsp extends Emitter
  6. {
  7. public $name = null;
  8. public $server = null;
  9. public $rooms = array();
  10. public $flags = array();
  11. public $sockets = array();
  12. public $connected = array();
  13. public $fns = array();
  14. public $ids = 0;
  15. public $acks = array();
  16. public static $events = array(
  17. 'connect' => 'connect', // for symmetry with client
  18. 'connection' => 'connection',
  19. 'newListener' => 'newListener'
  20. );
  21. //public static $flags = array('json','volatile');
  22. public function __construct($server, $name)
  23. {
  24. $this->name = $name;
  25. $this->server = $server;
  26. $this->initAdapter();
  27. Debug::debug('Nsp __construct');
  28. }
  29. public function __destruct()
  30. {
  31. Debug::debug('Nsp __destruct');
  32. }
  33. public function initAdapter()
  34. {
  35. $adapter_name = $this->server->adapter();
  36. $this->adapter = new $adapter_name($this);
  37. }
  38. public function to($name)
  39. {
  40. if(!isset($this->rooms[$name]))
  41. {
  42. $this->rooms[$name] = $name;
  43. }
  44. return $this;
  45. }
  46. public function in($name)
  47. {
  48. return $this->to($name);
  49. }
  50. public function add($client, $nsp, $fn)
  51. {
  52. $socket_name = $this->server->socket();
  53. $socket = new $socket_name($this, $client);
  54. if('open' === $client->conn->readyState)
  55. {
  56. $this->sockets[$socket->id]=$socket;
  57. $socket->onconnect();
  58. if(!empty($fn)) call_user_func($fn, $socket, $nsp);
  59. $this->emit('connect', $socket);
  60. $this->emit('connection', $socket);
  61. }
  62. else
  63. {
  64. echo('next called after client was closed - ignoring socket');
  65. }
  66. }
  67. /**
  68. * Removes a client. Called by each `Socket`.
  69. *
  70. * @api private
  71. */
  72. public function remove($socket)
  73. {
  74. // todo $socket->id
  75. unset($this->sockets[$socket->id]);
  76. }
  77. /**
  78. * Emits to all clients.
  79. *
  80. * @return {Namespace} self
  81. * @api public
  82. */
  83. public function emit($ev = null)
  84. {
  85. $args = func_get_args();
  86. if (isset(self::$events[$ev]))
  87. {
  88. call_user_func_array(array(__CLASS__, 'parent::emit'), $args);
  89. }
  90. else
  91. {
  92. // set up packet object
  93. $parserType = Parser::EVENT; // default
  94. //if (self::hasBin($args)) { $parserType = Parser::BINARY_EVENT; } // binary
  95. $packet = array('type'=> $parserType, 'data'=> $args );
  96. if (is_callable(end($args)))
  97. {
  98. echo('Callbacks are not supported when broadcasting');
  99. return;
  100. }
  101. $this->adapter->broadcast($packet, array(
  102. 'rooms'=> $this->rooms,
  103. 'flags'=> $this->flags
  104. ));
  105. $this->rooms = array();
  106. $this->flags = array();;
  107. }
  108. return $this;
  109. }
  110. public function send()
  111. {
  112. $args = func_get_args();
  113. array_unshift($args, 'message');
  114. $this->emit($args);
  115. return $this;
  116. }
  117. public function write()
  118. {
  119. $args = func_get_args();
  120. return call_user_func_array(array($this, 'send'), $args);
  121. }
  122. public function clients($fn)
  123. {
  124. $this->adapter->clients($this->rooms, $fn);
  125. return $this;
  126. }
  127. /**
  128. * Sets the compress flag.
  129. *
  130. * @param {Boolean} if `true`, compresses the sending data
  131. * @return {Socket} self
  132. * @api public
  133. */
  134. public function compress($compress)
  135. {
  136. $this->flags['compress'] = $compress;
  137. return $this;
  138. }
  139. }