ReconnCtrl.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * 资源加载控制器
  3. */
  4. cc.Class({
  5. extends: cc.Component,
  6. editor: {
  7. menu: 'Ctrl/ReconnCtrl'
  8. },
  9. properties: {
  10. },
  11. onLoad () {
  12. cc.game.on('e_socket_on_logined', this._handleSocketOnLogined, this);
  13. cc.game.on('e_socket_on_closed', this._handleSocketOnClosed, this);
  14. },
  15. onDestroy () {
  16. cc.game.targetOff(this);
  17. },
  18. _handleSocketOnLogined () {
  19. G.AppUtils.getSceneCtrl().loadingCtrl.hideTips();
  20. let antiaddictionInfo = G.AntiaddictionMgr.getAntiaddictionInfoForTime();
  21. if (antiaddictionInfo) {
  22. G.AppUtils.getSceneCtrl().showAntiaddiction(antiaddictionInfo);
  23. return;
  24. }
  25. if (G.SingleRoomMgr.isOpen()) {
  26. // 有单机场
  27. G.AppUtils.getSceneCtrl().closeAlert(JMC.ALERT_ID.HOME_NETWORK_ANOMALY);
  28. } else {
  29. // 无单机场
  30. if (G.AppUtils.getSceneCtrl().sceneName == '大厅') {
  31. G.AppUtils.getSceneCtrl().closeAlert(JMC.ALERT_ID.HOME_SCENE_NETWORK_ANOMALY);
  32. } else {
  33. G.AppUtils.getSceneCtrl().closeAlert(JMC.ALERT_ID.OTHER_SCENE_NETWORK_ANOMALY);
  34. }
  35. }
  36. },
  37. _handleSocketOnClosed (eventKey) {
  38. if (eventKey == 'call_close') {
  39. // 主动断开网络,不重连
  40. return;
  41. }
  42. if (G.LoginMgr.reconnectTimes == 0) {
  43. // 第一次断线直接重连socket
  44. G.AppUtils.getSceneCtrl().loadingCtrl.showTips('连接中...');
  45. this.scheduleOnce(() => {
  46. if (!G.LoginMgr.isOnline) {
  47. this._handleSocketOnClosed('call_reconnect');
  48. }
  49. }, 5);
  50. this.scheduleOnce(() => {
  51. // 重连计数
  52. G.LoginMgr.reconnectTimes += 1;
  53. // 重连
  54. G.NetworkMgr.reconnectSocket();
  55. }, 1);
  56. return;
  57. }
  58. if (G.LoginMgr.reconnectTimes >= G.LoginMgr.reconnectMaxTimes) {
  59. // 重连次数过多,让玩家回登录页走登录流程
  60. this.showServerMaintain(JMC.ALERT_ID.SERVER_MAINTAIN_ALERT);
  61. return;
  62. }
  63. if (G.SingleRoomMgr.isOpen()) {
  64. // 有单机场
  65. this.showNetworkAnomalySingle(JMC.ALERT_ID.HOME_NETWORK_ANOMALY);
  66. } else {
  67. // 无单机场
  68. if (G.AppUtils.getSceneCtrl().sceneName == '大厅') {
  69. this.showNetworkAnomaly(JMC.ALERT_ID.HOME_SCENE_NETWORK_ANOMALY);
  70. } else {
  71. this.showNetworkAnomaly(JMC.ALERT_ID.OTHER_SCENE_NETWORK_ANOMALY);
  72. }
  73. }
  74. },
  75. showServerMaintain (alertId) {
  76. if (G.AppUtils.getSceneCtrl().getAlert(alertId)) {
  77. return;
  78. }
  79. G.AppUtils.getSceneCtrl().popAllAlert();
  80. let cb = (alert, eventKey, eventData) => {
  81. switch (eventKey) {
  82. case 'clickedRight': {
  83. G.AppUtils.loadScene('scene_login');
  84. break;
  85. }
  86. case 'willShow': {
  87. // 兼容代码
  88. // 避免出现两个网络异常弹窗
  89. G.AppUtils.getSceneCtrl().popAllAlert(true);
  90. // ZIndex 设置100避免popAllAlert关闭
  91. alert.alertZIndex = 100;
  92. break;
  93. }
  94. default: {
  95. alert.alertDefaultCallback(eventKey, eventData);
  96. break;
  97. }
  98. }
  99. };
  100. let alertData = {};
  101. G.AppUtils.getSceneCtrl().showNormalAlert(alertId, alertData, cb);
  102. },
  103. showNetworkAnomaly (alertId) {
  104. if (G.AppUtils.getSceneCtrl().getAlert(alertId)) {
  105. return;
  106. }
  107. G.AppUtils.getSceneCtrl().popAllAlert();
  108. let cb = (alert, eventKey, eventData) => {
  109. switch (eventKey) {
  110. case 'clickedLeft': {
  111. G.AppUtils.loadScene('scene_login');
  112. break;
  113. }
  114. case 'clickedRight': {
  115. this.reconnect(alert);
  116. break;
  117. }
  118. case 'willShow': {
  119. // 兼容代码
  120. // 避免出现两个网络异常弹窗
  121. G.AppUtils.getSceneCtrl().popAllAlert(true);
  122. // ZIndex 设置100避免popAllAlert关闭
  123. alert.alertZIndex = 100;
  124. break;
  125. }
  126. default: {
  127. alert.alertDefaultCallback(eventKey, eventData);
  128. break;
  129. }
  130. }
  131. };
  132. let alertData = {};
  133. G.AppUtils.getSceneCtrl().showNormalAlert(alertId, alertData, cb);
  134. },
  135. reconnect (alert) {
  136. if (G.MiddleDevice.getApnType() == -1) {
  137. G.AppUtils.getSceneCtrl().addToast('当前未连接网络或网络异常');
  138. return;
  139. }
  140. let currTime = G.TimeUtils.getCurrentTime();
  141. let loginTimestamp = G.LoginMgr.loginTimestamp;
  142. if (!cc.sys.isNative && loginTimestamp && currTime - loginTimestamp >= 12 * 60 * 60) {
  143. // 非原生平台并且登录时间大于12小时
  144. // 回到授权场景
  145. G.AppUtils.loadScene('scene_login');
  146. return;
  147. } else {
  148. if (G.LoginMgr.loginTimestamp) {
  149. // 本次启动登录成功过
  150. let uid = G.UserMgr.getUid();
  151. let password = G.LoginMgr.getPassWord();
  152. G.LoginMgr.requestLogin(uid, password);
  153. // 重连计数
  154. G.LoginMgr.reconnectTimes += 1;
  155. G.AppUtils.getSceneCtrl().loadingCtrl.showTips('连接中...');
  156. this.scheduleOnce(() => {
  157. if (!G.LoginMgr.isOnline) {
  158. this._handleSocketOnClosed('call_reconnect');
  159. }
  160. }, 5);
  161. alert.close();
  162. } else {
  163. // 回到授权场景
  164. G.AppUtils.loadScene('scene_login');
  165. return;
  166. }
  167. }
  168. },
  169. // 处理离线
  170. handleOffline () {
  171. if (G.MiddleDevice.getApnType() == -1) {
  172. // 无网络需要弹出提示
  173. G.AppUtils.getSceneCtrl().addToast('当前未连接网络或网络异常');
  174. } else {
  175. if (G.LoginMgr.loginTimestamp) {
  176. // 登录成功过
  177. G.AppUtils.getSceneCtrl().loadingCtrl.showTips('连接中...', 5, () => {
  178. if (!G.LoginMgr.isOnline) {
  179. // reconnectTimes = 1 避免再次自动重连
  180. G.LoginMgr.reconnectTimes = 1;
  181. this._handleSocketOnClosed('call_reconnect');
  182. }
  183. });
  184. if (!G.NetworkMgr.isConnecting()) {
  185. // websocket为空的时候,发起重连
  186. // 解决回到大厅没有网络卡 连接中... 的问题
  187. G.NetworkMgr.reconnectSocket();
  188. }
  189. } else {
  190. // 无网络进入游戏
  191. // reconnectTimes = 1 避免再次自动重连
  192. G.LoginMgr.reconnectTimes = 1;
  193. this._handleSocketOnClosed('call_reconnect');
  194. }
  195. }
  196. },
  197. // 弹出断线重连弹窗
  198. showOfflineAlert () {
  199. this.showNetworkAnomaly(JMC.ALERT_ID.HOME_SCENE_NETWORK_ANOMALY);
  200. }
  201. });