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:----kHw3AcxD25RWPEYj+rV5UzPqkrS28arqdCMGTiBggz1rWhJogg9qQTcMFyVNOO2c7Q2cqnEeKPrLkzemsqGaV+rTRBmA3skRK/z8zq1pJFPeGDmdruLsRtbegSv/thoQgsYrW1ae1AtkMEJ6lxUATJtImvWWH3+LXO9zb6EAHbnNxP9pyaG8GJ7y0tr5wGGU7mRZdq+f4U6MYRq2c9ygh34Vh28KT5FYE20geSzjM0LS1PdCmiUXD4Rc7Yp5K7babd1GE2LiuGfFhCusVL7OlDusMPzhxR9EgJ1zzvaNc0r9qaTrnJyL+isqQtbGBpr4oNJfwEmgoz9oQ+AKnRL09oxrCUHRTEsmhjL6LaWDS5a+L9Lc1wEYmg653BatbFjMmxZG5a1AgOETtRLgvq4x67WoC57HQFx34kSF0O6MPUg6ImCw9VDQlsyVvHcgf9JjqraHFaQSwSOAMd22nV2mD6dOnHjE1OaSaHdkjmCxjol4qihvvARK3GtINqDmQW2XrJvTSMszO+TOsyVYWEZuzfoHu30WYZgApIdfnqwpVpCAAuRP6OFteLGUgngOY3xbN0DREOzuBQdQ2MY2x0EG2D7AKVkRoB8WEZiFVTqRy2AHqZyyoicqVRcVkSu1Zj7MUZJZjt8kJ18Wewf99XpireU3deJf1f7bd5J0UL3/LiI=----ATTACHMENT:----MzQxMjcwODM3NDQxMTY3MCA5MDM5NDE3MDMwMDk2NjEzIDU2MDA3MTE3NzA5MjMyNDg=