/** * 连接上服务器回调 */ public static function onConnect() { try { /** * 注册服务器 */ $rs = self::$rpc->Reg(self::$host); if ($rs) { # 返回成功 self::$host->key = $rs->key; self::$host->ip = $rs->ip; self::$host->id = $rs->id; if (is_array($rs->hosts) && $rs->hosts) { foreach ($rs->hosts as $host) { /** * @var Host $host */ $host->save(); } } # 保存数据, 其它 worker 进程就可以使用了 self::$host->save(); # 更新时间 Host::$lastChangeTime->set(time()); \MyQEE\Server\Server::$instance->info('register clusters host group: ' . self::$host->group . '#' . self::$host->id . '(' . self::$host->ip . ':' . self::$host->port . ') success.'); } else { throw new \Exception('result error.'); } } catch (\Exception $e) { \MyQEE\Server\Server::$instance->warn('register clusters host(' . self::$host->ip . ':' . self::$host->port . ') fail. ' . $e->getMessage()); } }
public function onClose($server, $fd, $fromId) { $host = Host::getHostByFd($fd); if ($host) { # 已经有连接上的服务器 Server::$instance->info("host#{$host->group}.{$host->id}({$host->ip}:{$host->port}) close connection."); $host->remove(); # 移除服务 foreach (Host::$table as $item) { if ($item['group'] === $host->group && $item['id'] === $host->id || $item['removed']) { continue; } # 通知所有客户端移除 RPC::factory($item['fd'], $item['from_id'])->trigger('server.remove', $host->group, $host->id); } } }