ReconnCtrl.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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_home');
  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. showNetworkAnomalySingle (alertId) {
  136. if (G.AppUtils.getSceneCtrl().getAlert(alertId)) {
  137. return;
  138. }
  139. G.AppUtils.getSceneCtrl().popAllAlert();
  140. let cb = (alert, eventKey, eventData) => {
  141. switch (eventKey) {
  142. case 'clickedLeft': {
  143. G.AppUtils.loadScene('scene_room_single');
  144. break;
  145. }
  146. case 'clickedRight': {
  147. this.reconnect(alert);
  148. break;
  149. }
  150. case 'willShow': {
  151. // 兼容代码
  152. // 避免出现两个网络异常弹窗
  153. G.AppUtils.getSceneCtrl().popAllAlert(true);
  154. // ZIndex 设置100避免popAllAlert关闭
  155. alert.alertZIndex = 100;
  156. break;
  157. }
  158. default: {
  159. alert.alertDefaultCallback(eventKey, eventData);
  160. break;
  161. }
  162. }
  163. };
  164. let alertData = {};
  165. G.AppUtils.getSceneCtrl().showNormalAlert(alertId, alertData, cb);
  166. },
  167. reconnect (alert) {
  168. if (G.MiddleDevice.getApnType() == -1) {
  169. if (G.SingleRoomMgr.isOpen()) {
  170. G.AppUtils.getSceneCtrl().addToast('当前未连接网络或网络异常,可先去单机挑战一下哦');
  171. } else {
  172. G.AppUtils.getSceneCtrl().addToast('当前未连接网络或网络异常');
  173. }
  174. return;
  175. }
  176. let currTime = G.TimeUtils.getCurrentTime();
  177. let loginTimestamp = G.LoginMgr.loginTimestamp;
  178. if (!cc.sys.isNative && loginTimestamp && currTime - loginTimestamp >= 12 * 60 * 60) {
  179. // 非原生平台并且登录时间大于12小时
  180. // 回到授权场景
  181. G.AppUtils.loadScene('scene_login');
  182. return;
  183. } else {
  184. if (G.LoginMgr.loginTimestamp) {
  185. // 本次启动登录成功过
  186. let uid = G.LoginMgr.getStorageValue('uid');
  187. let password = G.LoginMgr.getStorageValue('password');
  188. let isGuest = !G.LoginMgr.isAuthSuccess();
  189. G.LoginMgr.requestLogin(uid, password, isGuest);
  190. // 重连计数
  191. G.LoginMgr.reconnectTimes += 1;
  192. G.AppUtils.getSceneCtrl().loadingCtrl.showTips('连接中...');
  193. this.scheduleOnce(() => {
  194. if (!G.LoginMgr.isOnline) {
  195. this._handleSocketOnClosed('call_reconnect');
  196. }
  197. }, 5);
  198. alert.close();
  199. } else {
  200. // 回到授权场景
  201. G.AppUtils.loadScene('scene_login');
  202. return;
  203. }
  204. }
  205. },
  206. // 处理离线
  207. handleOffline () {
  208. if (G.MiddleDevice.getApnType() == -1) {
  209. // 无网络需要弹出提示
  210. if (G.SingleRoomMgr.isOpen()) {
  211. G.AppUtils.getSceneCtrl().addToast('当前未连接网络或网络异常,可先去单机挑战一下哦');
  212. } else {
  213. G.AppUtils.getSceneCtrl().addToast('当前未连接网络或网络异常');
  214. }
  215. } else {
  216. if (G.LoginMgr.loginTimestamp) {
  217. // 登录成功过
  218. G.AppUtils.getSceneCtrl().loadingCtrl.showTips('连接中...', 5, () => {
  219. if (!G.LoginMgr.isOnline) {
  220. // reconnectTimes = 1 避免再次自动重连
  221. G.LoginMgr.reconnectTimes = 1;
  222. this._handleSocketOnClosed('call_reconnect');
  223. }
  224. });
  225. if (!G.NetworkMgr.isConnecting()) {
  226. // websocket为空的时候,发起重连
  227. // 解决回到大厅没有网络卡 连接中... 的问题
  228. G.NetworkMgr.reconnectSocket();
  229. }
  230. } else {
  231. // 无网络进入游戏
  232. // reconnectTimes = 1 避免再次自动重连
  233. G.LoginMgr.reconnectTimes = 1;
  234. this._handleSocketOnClosed('call_reconnect');
  235. }
  236. }
  237. },
  238. // 弹出断线重连弹窗
  239. showOfflineAlert () {
  240. if (G.SingleRoomMgr.isOpen()) {
  241. // 有单机场
  242. this.showNetworkAnomalySingle(JMC.ALERT_ID.HOME_NETWORK_ANOMALY);
  243. } else {
  244. // 无单机场
  245. if (G.AppUtils.getSceneCtrl().sceneName == '大厅') {
  246. this.showNetworkAnomaly(JMC.ALERT_ID.HOME_SCENE_NETWORK_ANOMALY);
  247. } else {
  248. this.showNetworkAnomaly(JMC.ALERT_ID.OTHER_SCENE_NETWORK_ANOMALY);
  249. }
  250. }
  251. }
  252. });