You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
4.0 KiB

  1. IpManager = class("IpManager");
  2. -- 客户端本地会配置一些 urlList
  3. -- 启动游戏后会从服务器拉取新的 urlList 来覆盖本地的 urlList
  4. -- 轮番使用 urlList 中的 url 获取 ipList,并交给 net 使用
  5. -- 构造函数
  6. function IpManager:ctor()
  7. self.urlIndex = 0;
  8. self.urlList =
  9. {
  10. --"https://shibaipsource.dingdingqipai.com/iplist.php",
  11. "https://shibaip.dingdingqipai.com/iplist.php",
  12. "http://shibaip.dingdingqipai.com/iplist.php",
  13. "http://116.62.31.189:8090/iplist.php",
  14. }
  15. self:init();
  16. end
  17. -- 拉取所有的urlList
  18. function IpManager:init()
  19. local url;
  20. if PhpSetting and PhpSetting.ipServer then
  21. url = PhpSetting.ipServer
  22. else
  23. url = app.config.Setting.IpServer
  24. end
  25. print("IpManager:init() url = ", tostring(url))
  26. if not url or url == "" then
  27. return
  28. end
  29. local uid = loadUserId();
  30. local app = getAppId();
  31. local time = os.time();
  32. local union_id = "";
  33. local sign_key = "nh&109&%!ubx#"
  34. local sign = md5.sumhexa( app.."."..uid.."."..time.."."..sign_key )
  35. local params =
  36. {
  37. uid = uid,
  38. app = app,
  39. time = time,
  40. union_id = union_id,
  41. sign = sign,
  42. };
  43. local taskId;
  44. httpPost(url, params, function(status, response)
  45. -- taskId == nil 表示我已经将本次结果丢弃了
  46. if taskId == nil then
  47. return
  48. end
  49. taskId = nil
  50. if status ~= "successed" then
  51. return
  52. end
  53. local result = json.decode(response)
  54. print("result = ", table.tostring(result));
  55. if not result then
  56. showTooltip("Post返回数据为空")
  57. return
  58. end
  59. if result.configs and type(result.configs) == "table" then
  60. self.urlList = {}
  61. self.urlIndex = 0;
  62. for k,v in pairs(result.configs) do
  63. table.insert(self.urlList, v);
  64. end
  65. end
  66. log("IpManager:init() self.urlList = ", table.tostring(self.urlList));
  67. end,
  68. function(task_id)
  69. taskId = task_id;
  70. end)
  71. runDelay(5, function()
  72. if taskId then
  73. closeHttpPost(taskId);
  74. taskId = nil
  75. end
  76. end)
  77. end
  78. -- 更新IP列表
  79. -- 更新完成之后回调
  80. --[[
  81. callback = function( data )
  82. data =
  83. {
  84. {ip = "xxx", port = "xxx"},
  85. {ip = "xxx", port = "xxx"},
  86. {ip = "xxx", port = "xxx"},
  87. }
  88. --]]
  89. function IpManager:updateIpList(callback)
  90. log("200000 IpManager:updateIpList()")
  91. app.waitDialogManager:showWaitNetworkDialog("连接服务器2....")
  92. local function doCallback(data)
  93. app.waitDialogManager:closeWaitNetworkDialog()
  94. if callback then
  95. callback(data);
  96. end
  97. end
  98. -- 索引比数量还大,说明都已经用过了
  99. if self.urlIndex >= #self.urlList then
  100. doCallback(nil)
  101. return;
  102. end
  103. self.urlIndex = self.urlIndex + 1
  104. local url = self.urlList[self.urlIndex];
  105. if not url then
  106. doCallback({})
  107. return
  108. end
  109. local uid = loadUserId();
  110. local app = getAppId();
  111. local time = os.time();
  112. local union_id = "";
  113. local sign_key = "nh&109&%!ubx#"
  114. local sign = md5.sumhexa( app.."."..uid.."."..time.."."..sign_key )
  115. local params =
  116. {
  117. uid = uid,
  118. app = app,
  119. time = time,
  120. union_id = union_id,
  121. sign = sign,
  122. };
  123. log("200000 IpManager:updateIpList() url = ", url)
  124. log("200000 IpManager:updateIpList() params = ", table.tostring(params))
  125. local taskId;
  126. httpPost(url, params, function(status, response)
  127. log("200000 IpManager:updateIpList() status = ", status)
  128. log("200000 IpManager:updateIpList() response = ", tostring(response))
  129. -- taskId == nil 表示我已经将本次结果丢弃了
  130. if taskId == nil then
  131. return
  132. end
  133. taskId = nil
  134. if status ~= "successed" then
  135. self:updateIpList(callback);
  136. return
  137. end
  138. local result = json.decode(response)
  139. if result and result.servers and type(result.servers) == "table" then
  140. local ipList = {}
  141. for k,v in pairs(result.servers) do
  142. local arr = string.split(v, ":")
  143. local ip = arr[1]
  144. local port = arr[2]
  145. table.insert(ipList, {ip = ip, port = port});
  146. end
  147. doCallback(ipList)
  148. else
  149. doCallback({})
  150. end
  151. end,
  152. function(task_id)
  153. taskId = task_id;
  154. end)
  155. local timeOut = getNetTimeOutNum();
  156. runDelay(timeOut, function()
  157. if taskId then
  158. closeHttpPost(taskId);
  159. taskId = nil
  160. self:updateIpList(callback);
  161. end
  162. end)
  163. end
  164. return IpManager;