local EventSystem = require('Event/EventSystem') local JSON = require("cjson") local SDKUrlMonitor = {} local private = {} --5分支内 每个类型最多请求三次 第一次进队列但是不参与请求 local IntervalTime = 300 local MaxCount = 4 --当一个网络失败超过2次时 请求 local StartCount = 2 -- local monitorTimeList = {} local monitorInfo = nil local lock = false local CDN_DOWNFILE = "https://%s/public/speedtest.zip" --每个类型 五分钟只能只能尝试三次 function private.StartMonitor(monitorType, isAuto) isAuto = isAuto == nil or isAuto if isAuto then private.InitMonitorInfo() monitorInfo.isAuto = isAuto local monitor = monitorTimeList[monitorType] if monitor == nil then monitor = {} monitorTimeList[monitorType] = monitor end if #monitor == MaxCount then return else table.insert(monitor, UTL.Time.GetLocalTimestamp()) TimerUtil.Interval(private.IntervalRemove, IntervalTime, 1) if #monitor >= StartCount then private.StartNetTool(monitorType, true) end end else private.StartNetTool(monitorType, false) end end --设置客服玩家主动上报行为的信息 function private.SetServiceMonitorInfo() private.InitMonitorInfo() local str = JSON.encode(monitorInfo) SDKUtil.SetNetMonitorInfo(str) end function private.SetUrl(monitorType, url) private.InitMonitorInfo() if type(url) == "table" then for i,v in ipairs(url) do monitorInfo.domains[v] = monitorType..i end if monitorType == LuaConst.SDKMonitorType.CDN then monitorInfo.cdnUrl = string.format(CDN_DOWNFILE, url[1]) end else if monitorInfo == nil or monitorInfo.domains == nil or url == nil then return end monitorInfo.domains[url] = monitorType end end function private.StartNetTool(monitorType, isAuto) private.InitMonitorInfo() monitorInfo.isAuto = isAuto if monitorType == LuaConst.SDKMonitorTriggerType.GameConnect then monitorInfo.customInfo = JSON.encode({type = monitorType, levelId = BattleDataGetter.GetLevelID()}) else monitorInfo.customInfo = JSON.encode({type = monitorType}) end local str = JSON.encode(monitorInfo) logging.log(LTAG.LOG, " StartNetTool ", str) SDKUtil.StartNetTool(str) end function private.InitMonitorInfo() if monitorInfo == nil then monitorInfo = {areaId = GameAppInfo.Global_ProductId, domains = {}} end if MDL.Player and monitorInfo.roleId == nil then monitorInfo.roleId = tostring(MDL.Player.GetRoleId()) monitorInfo.roleName = BattleDataGetter.GetPlayerName() monitorInfo.serverId = MDL.Login.GetServerId() monitorInfo.serverName = MDL.Login.GetServerName() monitorInfo.uniqueId = BattleDataGetter.GetAcID() end end function private.IntervalRemove() local curTime = UTL.Time.GetLocalTimestamp() for _,monitor in pairs(monitorTimeList) do for i,v in ipairs(monitor) do if curTime - v >= IntervalTime then table.remove(monitor,i) end end end end function private.OnStartup() EventSystem.Regist(EventID.SDKUrlMonitor_ReqUrlFail, private.StartMonitor) EventSystem.Regist(EventID.SDKUrlMonitor_RegistMonitorUrl, private.SetUrl) EventSystem.Regist(EventID.SDKUrlMonitor_SetServiceMonitorInfo, private.SetServiceMonitorInfo) end private.OnStartup() return SDKUrlMonitor