local AppBase = class("AppBase") local t = 0 function AppBase:ctor(appName, packageRoot) cc.GameObject.extend(self) self:addComponent(require("luaScript.cc.components.behavior.EventProtocol"):new()):exportMethods() self.device = require("luaScript.cc.device"); self.name = appName self.packageRoot = packageRoot or "app" cc.Director:getInstance():getEventDispatcher():addCustomEventListener("applicationDidEnterBackground" , handler(self, self.applicationDidEnterBackground)); cc.Director:getInstance():getEventDispatcher():addCustomEventListener("applicationWillEnterForeground" , handler(self, self.applicationWillEnterForeground)); cc.Director:getInstance():getEventDispatcher():addCustomEventListener("applicationDidExit" , handler(self, self.applicationDidExit)); cc.Director:getInstance():getEventDispatcher():addCustomEventListener("applicationDidReceiveMemoryWarning" , handler(self, self.applicationDidReceiveMemoryWarning)); -- set global app app = self -- 标记是否进入后台 self.gIsBackground = false end function AppBase:run() end function AppBase:exit() CCDirector:sharedDirector():endToLua() os.exit() end function AppBase:applicationDidEnterBackground() log("AppBase:applicationDidEnterBackground()") --视图 PHP CMD 协议辅助打印 log("[进入 action moduleID[99]]-AppBase:applicationDidEnterBackground") self.gIsBackground = true self:dispatchEvent{ name = "applicationDidEnterBackground"} t = os.time() end function AppBase:applicationWillEnterForeground() log("AppBase:applicationWillEnterForeground()") --视图 PHP CMD 协议辅助打印 log("[进入 action moduleID[99]]-AppBase:applicationWillEnterForeground") -- 重置下时间 BeijingTime:reload(); if self.gIsBackground then local temp = os.time()-t log("moduleID[99] 从后台到前台间隔了:"..temp) --大于3600秒重启 if temp > 3600 then self.gIsBackground = false cc.Application:getInstance():restart() log("[进入 (超过1小时重启)action moduleID[99]]-AppBase:restart") return end -- 切到后台超60秒,客户端重新链接网络 local time = 60 if isDebug() then time = 10 end if temp > time and app.net and app.net:isInited() then print("app.net:reConnect()") log("[进入 (超过60秒会断网然后重连)action moduleID[99]]-AppBase:reConnect") app.net:close(); app.net:reConnect(); end end self:dispatchEvent{ name = "applicationWillEnterForeground"} self.gIsBackground = false; end function AppBase:applicationDidExit() --视图 PHP CMD 协议辅助打印 log("[进入 action moduleID[99]]-AppBase:applicationDidExit") self:dispatchEvent("applicationDidExit"); end function AppBase:applicationDidReceiveMemoryWarning() collectMemory(); end return AppBase