require("luaScript.Tools.NetBase") local NetGameServer = class("NetGameServer", NetBase) local IpStep = { fromConfig = 1, fromFile = 2, fromUrl = 3, } -- 构造函数 function NetGameServer:ctor() NetGameServer.super.ctor(self); -- 所有IP self.ipList = {} -- 当前IP的索引 self.ipIndex = 0; -- 当前IP使用的次数 self.curReconnectTimes = 0; -- 每个IP最多重连3次 self.maxReconnectTimes = 2; self.StringKey = "ServerInfo2" -- ip 管理器 -- require("luaScript.Tools.IpManager"); -- self.ipManager = IpManager:new(); -- 初始化默认使用的ip和端口 self:resetIpListFromConfig(); -- todo lwq 先使用服务器下发的地址,然后再使用本地的地址 self:resetIpListFromFile(); -- 优先从 url 获取Ip self.stepIdx = 1; --self.stepList = {IpStep.fromConfig, IpStep.fromUrl} self.stepList = {IpStep.fromConfig} end -- 重写:连接时的回调 function NetGameServer:OnConnecttingCallback() logD("moduleID[99] - NetGameServer:OnConnecttingCallback()") self.connectSuccess = false app:dispatchEvent({name = "onGameServerConnecting" , net = self}); end -- 重写:断开连接时的回调 function NetGameServer:OnDisconnectCallback() logD("moduleID[99] - NetGameServer:OnDisconnectCallback()") self.connectSuccess = false app:dispatchEvent({name = "onGameServerDisConnect" , net = self}); -- self:close() if not app.user.kickOut then app.user.kickOut = false self:reConnect(); end end -- 重写:连接成功之后的回调 function NetGameServer:OnConnectedCallback() logD("moduleID[99] - NetGameServer:OnConnectedCallback()") app:dispatchEvent({name = "onGameServerConnected" , net = self}); self:onConnectIpSave() end -- 将连接成功的ipList写入到本地文件 function NetGameServer:saveIpToFile() local str1 = self.ip..":"..self.port; saveUserInfo(self.StringKey, str1); end -- 重置 ipList 为本地文件中的值 function NetGameServer:resetIpListFromFile() log("200000 moduleID[99] NetGameServer:resetIpListFromFile()") self.ipIndex = 0; -- self.ipList = {}; local str1 = loadUserInfo(self.StringKey) if str1 and str1 ~= "" then local arr = string.split(str1, ":") if table.nums(arr) == 2 then if tonumber(arr[2]) > 0 then table.insert(self.ipList, {ip = arr[1], port = arr[2]}) return end end end end -- 重置 ipList 为配置的默认值 function NetGameServer:resetIpListFromConfig() log("200000 moduleID[99] NetGameServer:resetIpListFromConfig()") local strGameServer = "" if PhpSetting and PhpSetting.gameServer then strGameServer = PhpSetting.gameServer; end self.ipList = {}; local arr1 = string.split(strGameServer, "|") for k,v in pairs(arr1) do local arr2 = string.split(v, ":") table.insert(self.ipList, {ip=arr2[1], port=arr2[2]}); end end -- 重置获取ip的顺序 function NetGameServer:resetIpStep() -- 优先从 url 获取Ip self.stepIdx = 1; --self.stepList = {IpStep.fromUrl, IpStep.fromConfig} self.stepList = {IpStep.fromConfig} end -- 更新可用的ipList -- callback = function() --[[function NetGameServer:upateIpList(callback) log("200000 NetGameServer:upateIpList()") local function doCallback(ret) if callback then callback(ret); end end self.ipList = {} self.ipIndex = 0; local curStep = self.stepList[self.stepIdx]; if not curStep then doCallback(false); return end log("200000 NetGameServer:upateIpList() curStep = ", curStep) if curStep == IpStep.fromUrl then if self.ipManager then local function onUpdateIpListResponse(ipList) log("200000 NetGameServer:resetIpListFromFile() updateIpList --> ipList = ", table.tostring(ipList)) if ipList and type(ipList) == "table" then for k,v in pairs(ipList) do table.insert(self.ipList, {ip = v.ip, port = v.port}); end else self.stepIdx = self.stepIdx + 1 end doCallback(true); end self.ipManager:updateIpList(onUpdateIpListResponse); end elseif curStep == IpStep.fromConfig then self.stepIdx = self.stepIdx + 1 self:resetIpListFromConfig(); doCallback(true); elseif curStep == IpStep.fromFile then self.stepIdx = self.stepIdx + 1 self:resetIpListFromFile(); doCallback(true); else doCallback(true); end end--]] -- 获取一个可用使用的ip和port -- callback = function(ip, port) --[[function NetGameServer:getIp(callback) log("200000 NetGameServer:getIp() ") local function doCallback(ip, port) if callback then callback(ip, port); end end local function getFromList() log("200000 NetGameServer:getIp() getFromList()") self.ipIndex = self.ipIndex + 1 if self.ipIndex <= #self.ipList then doCallback(self.ipList[self.ipIndex].ip, self.ipList[self.ipIndex].port); else doCallback(nil, nil) end end local function getWithUpdate() log("200000 NetGameServer:getIp() getWithUpdate()") local function onUpdateEnd(ret) if #self.ipList > 0 then getFromList(); else if ret then self:upateIpList(onUpdateEnd) else doCallback(nil, nil) end end end self:upateIpList(onUpdateEnd) end log("200000 NetGameServer:getIp() ", self.ip, self.port, self.curReconnectTimes, self.maxReconnectTimes) if self.curReconnectTimes < self.maxReconnectTimes and self.ip and self.port then log("200000 NetGameServer:getIp() use old") doCallback(self.ip, self.port) else self.curReconnectTimes = 0 if self.ipList and self.ipIndex < #self.ipList then getFromList(); else getWithUpdate(); end end end--]] function NetGameServer:getIp(callback) log("200000 moduleID[99] NetGameServer:getIp() ") log("200000 moduleID[99] NetGameServer:getIp() ipList = ", table.tostring(self.ipList)) local function doCallback(ip, port) if callback then callback(ip, port); end end self.ipIndex = self.ipIndex + 1 if self.ipIndex <= #self.ipList then log("200000 moduleID[99] NetGameServer:getIp() "..self.ipIndex) if not self.ipList[self.ipIndex] then doCallback(nil ,nil) return end local tip = self.ipList[self.ipIndex].ip local tport = self.ipList[self.ipIndex].port doCallback(tip,tport) else doCallback(nil ,nil) end end -- 开始连接 function NetGameServer:startNet() logD("moduleID[99] - NetGameServer:startNet()") self.isNeedReconnect = true self:getIp(function(ip, port) if ip and port then self.curReconnectTimes = self.curReconnectTimes + 1 self:connect(ip, port) else -- ip 不可用,提示玩家重启游戏 local function onOk() cc.Application:getInstance():restart(); end app.waitDialogManager:closeAllNetWait(); showConfirmDialog("网络连接失败,点击确定重启游戏!", onOk); end end ) end -- 重新连接服务器 function NetGameServer:reConnect() logD("moduleID[99] - NetGameServer:reConnect()") app.waitDialogManager:closeAllNetWait(); app.waitDialogManager:showWaitNetworkDialog("重连中....") if not self.isNeedReconnect then return; end self:startNet(); end --连上IP 存储ip function NetGameServer:onConnectIpSave()--这些操作是之前每次收到服务器回复都会执行,现在提出来,只在连上的时候执行一次 logD("moduleID[99] NetGameServer:onConnectIpSave()") self.ipIndex = 0 -- 标记为成功链接 self.connectSuccess = true -- 能收到服务器下发的消息,才说明是真的连接上了 self.curReconnectTimes = 0; -- 重置 ipManager 的索引 if self.ipManager then self.ipManager.urlIndex = 0; end -- 将连接成功的ip写入到本地文件 self:saveIpToFile(); -- 重置执行队列 self:resetIpStep() end function NetGameServer:onRecvServerMsg(moduleID , cmdID , statusCode , stream) -- 发送事件 app:dispatchEvent({name = "onRecvMsgFromGameServer" , net = self}); end return NetGameServer;