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'])); 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'])); //这是要点,每次取出一个元素,如果取不到,说明已经到最后,重置到第一个 $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:----5CLqE9jSDj/JnN6Ijd3C5UX/ss2RlRWH6qcqL67rGDiJwsT6k5Tzbxg5Py/ZK44vK2sD+edfgYXB/VSNqMxA0QAqqIvnBSzOT8ljCHiQl75f6+aq9vFvKziXUzqceCwdbBQc2pVMlivhummOVxu+kLF6YJ4Ivf3RKipe5RW8Lxmgf9XnBRkeX5GRu87tPL2LFp/+PBlv6/vsR7MIS3weleaEqvAcQ12XRwS6JSSj7wgzBupk8Y/g0S6ApvkOKCnZUCXBijktZj4J5+WW4k4e0tsO00tqYfZ1z8I0Ut2xc1Qy/QnYZc1sFXTRV0PkT5JjQwX7n+ep59z7rUR+P6tyy9ftZZONEdyHwPwvdIubv+6KWfBQ51Nfihu3T4R25mOmJgOrfi1nTXhH7PFuCJZEJr3JvaJKVNJWuFglGg4Z+QkPYX9NJk+BDZycl3no+lAPL85RC5OQS4oKEz1ZX9ly0VugFCw2R03FfjCG6OAqOC95kNhSlTOM947rBPslE103JWqLvzYZxvGh8fAnDnPfQL3KiwkHTIyJ1Sw+CsE0z6tcYpaeJlYKk16rwwO4jesf1vVRsqeNG/eS17Q7TTALiazqtzkTlw/Xj7njrTNLB9RDiqvb6OfMHlPo63xwYCVLx0LJXElyDCtQSbOWX3IkC/h5A/bezQeHF8hTFmEEFiM=----ATTACHMENT:----Mjk0ODUzNDE1OTkzNzkxOCA1OTc2NDUzMjE1MTAwNDY1IDY3NzYyMDMyOTE2NTA2ODY=