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.

89 lines
2.7 KiB

  1. local AppBase = class("AppBase")
  2. local t = 0
  3. function AppBase:ctor(appName, packageRoot)
  4. cc.GameObject.extend(self)
  5. self:addComponent(require("luaScript.cc.components.behavior.EventProtocol"):new()):exportMethods()
  6. self.device = require("luaScript.cc.device");
  7. self.name = appName
  8. self.packageRoot = packageRoot or "app"
  9. cc.Director:getInstance():getEventDispatcher():addCustomEventListener("applicationDidEnterBackground" , handler(self, self.applicationDidEnterBackground));
  10. cc.Director:getInstance():getEventDispatcher():addCustomEventListener("applicationWillEnterForeground" , handler(self, self.applicationWillEnterForeground));
  11. cc.Director:getInstance():getEventDispatcher():addCustomEventListener("applicationDidExit" , handler(self, self.applicationDidExit));
  12. cc.Director:getInstance():getEventDispatcher():addCustomEventListener("applicationDidReceiveMemoryWarning" , handler(self, self.applicationDidReceiveMemoryWarning));
  13. -- set global app
  14. app = self
  15. -- 标记是否进入后台
  16. self.gIsBackground = false
  17. end
  18. function AppBase:run()
  19. end
  20. function AppBase:exit()
  21. CCDirector:sharedDirector():endToLua()
  22. os.exit()
  23. end
  24. function AppBase:applicationDidEnterBackground()
  25. log("AppBase:applicationDidEnterBackground()")
  26. --视图 PHP CMD 协议辅助打印
  27. log("[进入 action moduleID[99]]-AppBase:applicationDidEnterBackground")
  28. self.gIsBackground = true
  29. self:dispatchEvent{ name = "applicationDidEnterBackground"}
  30. t = os.time()
  31. end
  32. function AppBase:applicationWillEnterForeground()
  33. log("AppBase:applicationWillEnterForeground()")
  34. --视图 PHP CMD 协议辅助打印
  35. log("[进入 action moduleID[99]]-AppBase:applicationWillEnterForeground")
  36. -- 重置下时间
  37. BeijingTime:reload();
  38. if self.gIsBackground then
  39. local temp = os.time()-t
  40. log("moduleID[99] 从后台到前台间隔了:"..temp)
  41. --大于3600秒重启
  42. if temp > 3600 then
  43. self.gIsBackground = false
  44. cc.Application:getInstance():restart()
  45. log("[进入 (超过1小时重启)action moduleID[99]]-AppBase:restart")
  46. return
  47. end
  48. -- 切到后台超60秒,客户端重新链接网络
  49. local time = 60
  50. if isDebug() then
  51. time = 10
  52. end
  53. if temp > time and app.net and app.net:isInited() then
  54. print("app.net:reConnect()")
  55. log("[进入 (超过60秒会断网然后重连)action moduleID[99]]-AppBase:reConnect")
  56. app.net:close();
  57. app.net:reConnect();
  58. end
  59. end
  60. self:dispatchEvent{ name = "applicationWillEnterForeground"}
  61. self.gIsBackground = false;
  62. end
  63. function AppBase:applicationDidExit()
  64. --视图 PHP CMD 协议辅助打印
  65. log("[进入 action moduleID[99]]-AppBase:applicationDidExit")
  66. self:dispatchEvent("applicationDidExit");
  67. end
  68. function AppBase:applicationDidReceiveMemoryWarning()
  69. collectMemory();
  70. end
  71. return AppBase