IpManager = class("IpManager"); -- 客户端本地会配置一些 urlList -- 启动游戏后会从服务器拉取新的 urlList 来覆盖本地的 urlList -- 轮番使用 urlList 中的 url 获取 ipList,并交给 net 使用 -- 构造函数 function IpManager:ctor() self.urlIndex = 0; self.urlList = { --"https://shibaipsource.dingdingqipai.com/iplist.php", "https://shibaip.dingdingqipai.com/iplist.php", "http://shibaip.dingdingqipai.com/iplist.php", "http://116.62.31.189:8090/iplist.php", } self:init(); end -- 拉取所有的urlList function IpManager:init() local url; if PhpSetting and PhpSetting.ipServer then url = PhpSetting.ipServer else url = app.config.Setting.IpServer end print("IpManager:init() url = ", tostring(url)) if not url or url == "" then return end local uid = loadUserId(); local app = getAppId(); local time = os.time(); local union_id = ""; local sign_key = "nh&109&%!ubx#" local sign = md5.sumhexa( app.."."..uid.."."..time.."."..sign_key ) local params = { uid = uid, app = app, time = time, union_id = union_id, sign = sign, }; local taskId; httpPost(url, params, function(status, response) -- taskId == nil 表示我已经将本次结果丢弃了 if taskId == nil then return end taskId = nil if status ~= "successed" then return end local result = json.decode(response) print("result = ", table.tostring(result)); if not result then showTooltip("Post返回数据为空") return end if result.configs and type(result.configs) == "table" then self.urlList = {} self.urlIndex = 0; for k,v in pairs(result.configs) do table.insert(self.urlList, v); end end log("IpManager:init() self.urlList = ", table.tostring(self.urlList)); end, function(task_id) taskId = task_id; end) runDelay(5, function() if taskId then closeHttpPost(taskId); taskId = nil end end) end -- 更新IP列表 -- 更新完成之后回调 --[[ callback = function( data ) data = { {ip = "xxx", port = "xxx"}, {ip = "xxx", port = "xxx"}, {ip = "xxx", port = "xxx"}, } --]] function IpManager:updateIpList(callback) log("200000 IpManager:updateIpList()") app.waitDialogManager:showWaitNetworkDialog("连接服务器2....") local function doCallback(data) app.waitDialogManager:closeWaitNetworkDialog() if callback then callback(data); end end -- 索引比数量还大,说明都已经用过了 if self.urlIndex >= #self.urlList then doCallback(nil) return; end self.urlIndex = self.urlIndex + 1 local url = self.urlList[self.urlIndex]; if not url then doCallback({}) return end local uid = loadUserId(); local app = getAppId(); local time = os.time(); local union_id = ""; local sign_key = "nh&109&%!ubx#" local sign = md5.sumhexa( app.."."..uid.."."..time.."."..sign_key ) local params = { uid = uid, app = app, time = time, union_id = union_id, sign = sign, }; log("200000 IpManager:updateIpList() url = ", url) log("200000 IpManager:updateIpList() params = ", table.tostring(params)) local taskId; httpPost(url, params, function(status, response) log("200000 IpManager:updateIpList() status = ", status) log("200000 IpManager:updateIpList() response = ", tostring(response)) -- taskId == nil 表示我已经将本次结果丢弃了 if taskId == nil then return end taskId = nil if status ~= "successed" then self:updateIpList(callback); return end local result = json.decode(response) if result and result.servers and type(result.servers) == "table" then local ipList = {} for k,v in pairs(result.servers) do local arr = string.split(v, ":") local ip = arr[1] local port = arr[2] table.insert(ipList, {ip = ip, port = port}); end doCallback(ipList) else doCallback({}) end end, function(task_id) taskId = task_id; end) local timeOut = getNetTimeOutNum(); runDelay(timeOut, function() if taskId then closeHttpPost(taskId); taskId = nil self:updateIpList(callback); end end) end return IpManager;