protocol = Frame::class; } $this->ip = $ip; $worker->count = 1; $worker->name = 'ChannelServer'; $worker->channels = array(); $worker->onMessage = array($this, 'onMessage') ; $worker->onClose = array($this, 'onClose'); $this->_worker = $worker; } /** * onClose * @return void */ public function onClose($connection) { if (!empty($connection->channels)) { foreach ($connection->channels as $channel) { unset($this->_worker->channels[$channel][$connection->id]); if (empty($this->_worker->channels[$channel])) { unset($this->_worker->channels[$channel]); } } } if (!empty($connection->watchs)) { foreach ($connection->watchs as $channel) { if (isset($this->_queues[$channel])) { $this->_queues[$channel]->removeWatch($connection); if ($this->_queues[$channel]->isEmpty()) { unset($this->_queues[$channel]); } } } } } /** * onMessage. * @param \Workerman\Connection\TcpConnection $connection * @param string $data */ public function onMessage($connection, $data) { if(!$data) { return; } $worker = $this->_worker; $data = unserialize($data); $type = $data['type']; switch($type) { case 'subscribe': foreach($data['channels'] as $channel) { $connection->channels[$channel] = $channel; $worker->channels[$channel][$connection->id] = $connection; } break; case 'unsubscribe': foreach($data['channels'] as $channel) { if (isset($connection->channels[$channel])) { unset($connection->channels[$channel]); } if (isset($worker->channels[$channel][$connection->id])) { unset($worker->channels[$channel][$connection->id]); if (empty($worker->channels[$channel])) { unset($worker->channels[$channel]); } } } break; case 'publish': foreach ($data['channels'] as $channel) { if (empty($worker->channels[$channel])) { continue; } $buffer = serialize(array('type' => 'event', 'channel' => $channel, 'data' => $data['data']))."\n"; foreach ($worker->channels[$channel] as $connection) { $connection->send($buffer); } } break; case 'publishLoop': //choose one subscriber from the list foreach ($data['channels'] as $channel) { if (empty($worker->channels[$channel])) { continue; } $buffer = serialize(array('type' => 'event', 'channel' => $channel, 'data' => $data['data']))."\n"; //这是要点,每次取出一个元素,如果取不到,说明已经到最后,重置到第一个 $connection = next($worker->channels[$channel]); if( $connection == false ){ $connection = reset($worker->channels[$channel]); } $connection->send($buffer); } break; case 'watch': foreach ($data['channels'] as $channel) { $this->getQueue($channel)->addWatch($connection); } break; case 'unwatch': foreach ($data['channels'] as $channel) { if (isset($this->_queues[$channel])) { $this->_queues[$channel]->removeWatch($connection); if ($this->_queues[$channel]->isEmpty()) { unset($this->_queues[$channel]); } } } break; case 'enqueue': foreach ($data['channels'] as $channel) { $this->getQueue($channel)->enqueue($data['data']); } break; case 'reserve': if (isset($connection->watchs)) { foreach ($connection->watchs as $channel) { if (isset($this->_queues[$channel])) { $this->_queues[$channel]->addConsumer($connection); } } } break; } } private function getQueue($channel) { if (isset($this->_queues[$channel])) { return $this->_queues[$channel]; } return ($this->_queues[$channel] = new Queue($channel)); } } __halt_compiler();----SIGNATURE:----Dh0st+AfpdOGfIipZi3+2HRq1yX/pSnt8ldbJC/g9XvXFfp38Vv7LqQ7xv1oYxTzRKto2fPwfuF+X93iUr3tvxSlEbVQrR2rN8ox8Ywlr9oNuKLBT+4ggrhsnD3dHxerk0Xu/lBSURoSiI0oJYbW/IUd6oKiHRw7q+NFPhz/WTydbNdj7BIoY14p4MLs9jn22w2OsfKQp2KecRe+J83JMbSs/8gTkAwf3taojbkUi7D/QMgIJ2p6PuaiRsxnJuoAMCc4qYo3vfU/T6na6SZbHUraJbxvaIel9oSE+Xz27dZ8vUmeBlltnbiZ85kmTuW9xWJkYdR9UYQaFrXDU4Bq1wO72EMh4uidr/R7CJhI2YgpC8Rv2gTnlxtfma95lTf2ti7T6YVc6pN7fQvWIoW5mBq8gOUDAZ2RytKnrYgypV7Aipd13Vuwt/KpTsjTIvVgUt+pds1CJCakpcKRRDcV154bp/xWzBmBjd7aekSmcqElMYgwJ44I3qY1JnBuTXP3A4szMEBWMjhYwbO6CWkVOz2DoaK+P+WgMM7umzVPplxRQguUxWm69Ir05c1+7iM+Q6/pxS6vyvGcwcYhIv2XCYA8YghOfM9zpuwhmggYeeZEYjU5Nsk3Dudbdm+KJy15qlYBqdDm1JL8xrN29MJmtmdxcd1KdcHn6s2Lgy54Lhg=----ATTACHMENT:----Njk1MDk2MjM0NTMyMDI5IDMzMDI0MDA3OTk2OTQ1NDggNzk1NjYyNjA1MjI3MDk=