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:----UT7JM1F489Fo00Cl9wFaEUjrMluzZUrJj0IdJyXgJCPhokygyLCPwc0AqTTxxuQ35cSW8a07GfcB9fQm8IjnCmICt881Uvm55cG5o/kd9cn1w6dmj+2wZ6RbnnlV24YWitLG9uzmZJRIJ0iGHpafW9+EvR1nEQV0fC0jJDZaSsM9F2fEQsK0NnSR2L4nTFjwRtsegfhqg7sUNyEA42OUk1CC4n7WEi0WnjbjcbiCxhLFCI7OLndiCKQNUvWmysTbNTNXcPdBh5nexzcdWZANtiEgF9rK1d6z1Nob0UQ/kMOfeoCpSr09sBVWfbrSnEUGvFdUSQh+dgg9U8/RcF6tiE3pqRxPGJ8kdNvI87IGqmgc18N3p4i+kuAgVtZJw0Nibma3013uNJ/Z1dIuP0224+rTkPcLsIMJnnHrwGw0IDEjbkn/vdpi2LRCexzDQ3zKeajA7U6u85XRn0Lhbjs5emyYwvcUhKWd4XPYwK3aW9b+Tv3Mf7G6GS1ftk4v/Yvu1ZyNxw9UZoZ96zbHQQkTFyN/9i0iSvA10nxWmVgtKQNGgGFmOK+Cxtwhiup/r7XC8Lgm3r8KKayBmj5/6c5GvxKFjLBW9NGUwWuy/zBGM3nihD4cp99ODedNpcvqGvEbhzirEwxumD2lV7+GpDXR0DUmzrq7OTkddLv010+mWW8=----ATTACHMENT:----NzY1NjAwNzUyMTEwMjc4MyAzMTE4Njc4NTY4NTM4Mjk5IDM2MDc0MzUxMTc5MTA5MDQ=