ChannelAdapter.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace PHPSocketIO;
  3. class ChannelAdapter extends DefaultAdapter
  4. {
  5. protected $_channelId = null;
  6. public static $ip = '127.0.0.1';
  7. public static $port = 2206;
  8. public function __construct($nsp)
  9. {
  10. parent::__construct($nsp);
  11. $this->_channelId = (function_exists('random_int') ? random_int(1, 10000000): rand(1, 10000000)) . "-" . (function_exists('posix_getpid') ? posix_getpid(): 1);
  12. \Channel\Client::connect(self::$ip, self::$port);
  13. \Channel\Client::$onMessage = array($this, 'onChannelMessage');
  14. \Channel\Client::subscribe("socket.io#/#");
  15. Debug::debug('ChannelAdapter __construct');
  16. }
  17. public function __destruct()
  18. {
  19. Debug::debug('ChannelAdapter __destruct');
  20. }
  21. public function add($id ,$room)
  22. {
  23. $this->sids[$id][$room] = true;
  24. $this->rooms[$room][$id] = true;
  25. $channel = "socket.io#/#$room#";
  26. \Channel\Client::subscribe($channel);
  27. }
  28. public function del($id, $room)
  29. {
  30. unset($this->sids[$id][$room]);
  31. unset($this->rooms[$room][$id]);
  32. if(empty($this->rooms[$room]))
  33. {
  34. unset($this->rooms[$room]);
  35. $channel = "socket.io#/#$room#";
  36. \Channel\Client::unsubscribe($channel);
  37. }
  38. }
  39. public function delAll($id)
  40. {
  41. $rooms = isset($this->sids[$id]) ? array_keys($this->sids[$id]) : array();
  42. if($rooms)
  43. {
  44. foreach($rooms as $room)
  45. {
  46. if(isset($this->rooms[$room][$id]))
  47. {
  48. unset($this->rooms[$room][$id]);
  49. $channel = "socket.io#/#$room#";
  50. \Channel\Client::unsubscribe($channel);
  51. }
  52. if(isset($this->rooms[$room]) && empty($this->rooms[$room]))
  53. {
  54. unset($this->rooms[$room]);
  55. }
  56. }
  57. }
  58. unset($this->sids[$id]);
  59. }
  60. public function onChannelMessage($channel, $msg)
  61. {
  62. if($this->_channelId === array_shift($msg))
  63. {
  64. //echo "ignore same channel_id \n";
  65. return;
  66. }
  67. $packet = $msg[0];
  68. $opts = $msg[1];
  69. if(!$packet)
  70. {
  71. echo "invalid channel:$channel packet \n";
  72. return;
  73. }
  74. if(empty($packet['nsp']))
  75. {
  76. $packet['nsp'] = '/';
  77. }
  78. if($packet['nsp'] != $this->nsp->name)
  79. {
  80. echo "ignore different namespace {$packet['nsp']} != {$this->nsp->name}\n";
  81. return;
  82. }
  83. $this->broadcast($packet, $opts, true);
  84. }
  85. public function broadcast($packet, $opts, $remote = false)
  86. {
  87. parent::broadcast($packet, $opts);
  88. if (!$remote)
  89. {
  90. $packet['nsp'] = '/';
  91. if(!empty($opts['rooms']))
  92. {
  93. foreach($opts['rooms'] as $room)
  94. {
  95. $chn = "socket.io#/#$room#";
  96. $msg = array($this->_channelId, $packet, $opts);
  97. \Channel\Client::publish($chn, $msg);
  98. }
  99. }
  100. else
  101. {
  102. $chn = "socket.io#/#";
  103. $msg = array($this->_channelId, $packet, $opts);
  104. \Channel\Client::publish($chn, $msg);
  105. }
  106. }
  107. }
  108. }