SocketIO.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace PHPSocketIO;
  3. use Workerman\Worker;
  4. use PHPSocketIO\Engine\Engine;
  5. class SocketIO
  6. {
  7. public $nsps = array();
  8. protected $_nsp = null;
  9. protected $_socket = null;
  10. protected $_adapter = null;
  11. public $eio = null;
  12. public $engine = null;
  13. protected $_origins = '*:*';
  14. protected $_path = null;
  15. public function __construct($port = null, $opts = array())
  16. {
  17. $nsp = isset($opts['nsp']) ? $opts['nsp'] : '\PHPSocketIO\Nsp';
  18. $this->nsp($nsp);
  19. $socket = isset($opts['socket']) ? $opts['socket'] : '\PHPSocketIO\Socket';
  20. $this->socket($socket);
  21. $adapter = isset($opts['adapter']) ? $opts['adapter'] : '\PHPSocketIO\DefaultAdapter';
  22. $this->adapter($adapter);
  23. if(isset($opts['origins']))
  24. {
  25. $this->origins($opts['origins']);
  26. }
  27. unset($opts['nsp'], $opts['socket'], $opts['adapter'], $opts['origins']);
  28. $this->sockets = $this->of('/');
  29. if(!class_exists('Protocols\SocketIO'))
  30. {
  31. class_alias('PHPSocketIO\Engine\Protocols\SocketIO', 'Protocols\SocketIO');
  32. }
  33. if($port)
  34. {
  35. $worker = new Worker('SocketIO://0.0.0.0:'.$port, $opts);
  36. $worker->name = 'PHPSocketIO';
  37. if(isset($opts['ssl'])) {
  38. $worker->transport = 'ssl';
  39. }
  40. $this->attach($worker);
  41. }
  42. }
  43. public function nsp($v = null)
  44. {
  45. if (empty($v)) return $this->_nsp;
  46. $this->_nsp = $v;
  47. return $this;
  48. }
  49. public function socket($v = null)
  50. {
  51. if (empty($v)) return $this->_socket;
  52. $this->_socket = $v;
  53. return $this;
  54. }
  55. public function adapter($v = null)
  56. {
  57. if (empty($v)) return $this->_adapter;
  58. $this->_adapter = $v;
  59. foreach($this->nsps as $nsp)
  60. {
  61. $nsp->initAdapter();
  62. }
  63. return $this;
  64. }
  65. public function origins($v = null)
  66. {
  67. if ($v === null) return $this->_origins;
  68. $this->_origins = $v;
  69. if(isset($this->engine)) {
  70. $this->engine->origins = $this->_origins;
  71. }
  72. return $this;
  73. }
  74. public function attach($srv, $opts = array())
  75. {
  76. $engine = new Engine();
  77. $this->eio = $engine->attach($srv, $opts);
  78. // Export http server
  79. $this->worker = $srv;
  80. // bind to engine events
  81. $this->bind($engine);
  82. return $this;
  83. }
  84. public function bind($engine)
  85. {
  86. $this->engine = $engine;
  87. $this->engine->on('connection', array($this, 'onConnection'));
  88. $this->engine->origins = $this->_origins;
  89. return $this;
  90. }
  91. public function of($name, $fn = null)
  92. {
  93. if($name[0] !== '/')
  94. {
  95. $name = "/$name";
  96. }
  97. if(empty($this->nsps[$name]))
  98. {
  99. $nsp_name = $this->nsp();
  100. $this->nsps[$name] = new $nsp_name($this, $name);
  101. }
  102. if ($fn)
  103. {
  104. $this->nsps[$name]->on('connect', $fn);
  105. }
  106. return $this->nsps[$name];
  107. }
  108. public function onConnection($engine_socket)
  109. {
  110. $client = new Client($this, $engine_socket);
  111. $client->connect('/');
  112. return $this;
  113. }
  114. public function on()
  115. {
  116. $args = array_pad(func_get_args(), 2, null);
  117. if ($args[0] === 'workerStart') {
  118. $this->worker->onWorkerStart = $args[1];
  119. } else if ($args[0] === 'workerStop') {
  120. $this->worker->onWorkerStop = $args[1];
  121. } else if ($args[0] !== null) {
  122. return call_user_func_array(array($this->sockets, 'on'), $args);
  123. }
  124. }
  125. public function in()
  126. {
  127. return call_user_func_array(array($this->sockets, 'in'), func_get_args());
  128. }
  129. public function to()
  130. {
  131. return call_user_func_array(array($this->sockets, 'to'), func_get_args());
  132. }
  133. public function emit()
  134. {
  135. return call_user_func_array(array($this->sockets, 'emit'), func_get_args());
  136. }
  137. public function send()
  138. {
  139. return call_user_func_array(array($this->sockets, 'send'), func_get_args());
  140. }
  141. public function write()
  142. {
  143. return call_user_func_array(array($this->sockets, 'write'), func_get_args());
  144. }
  145. }