NetworkReconnector.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { _decorator, Component, Director, director, Node } from 'cc';
  2. import { gameNet, lobbyNet, NetGameServer, NetLobbyServer } from './NetGameServer';
  3. import { WebsocketClient } from './WebsocketClient';
  4. import { GameSceneUtil } from './GameSceneUtil';
  5. import { UserMgr } from './UserMgr';
  6. import { SceneDef } from '../../scripts/SceneDef';
  7. import { RoomMgr } from './RoomMgr';
  8. import { EDITOR, PREVIEW } from 'cc/env';
  9. import { GameMgr } from './GameMgr';
  10. import { LobbyMgr } from './LobbyMgr';
  11. const { ccclass, property } = _decorator;
  12. //主动添加到所有场景。
  13. director.on(Director.EVENT_AFTER_SCENE_LAUNCH, () => {
  14. if (EDITOR && PREVIEW || !EDITOR) {
  15. if (director.getScene().getChildByName('NetworkReconnector')) {
  16. throw new Error('NetworkReconnector can only be added by code, please remove from scene:.' + director.getScene().name);
  17. }
  18. let node = new Node('NetworkReconnector');
  19. node.addComponent(NetworkReconnector);
  20. director.getScene().addChild(node);
  21. }
  22. });
  23. @ccclass('NetworkReconnector')
  24. export class NetworkReconnector extends Component {
  25. start() {
  26. this.schedule(() => {
  27. this.checkNetwork();
  28. }, 1.0);
  29. }
  30. public get isInGame() {
  31. return director.getScene().name.indexOf('game_') == 0;
  32. }
  33. private _tryTimes = 0;
  34. needReconnect(v: { code?: number, reason?: string, isManual?: boolean }) {
  35. if(!v){
  36. return true;
  37. }
  38. if (v.reason == 'login_to_other') {
  39. this.enabled = false;
  40. tgx.UIAlert.show('Account is logged in on other devices!').onClick(() => {
  41. tgx.SceneUtil.loadScene(SceneDef.LOGIN);
  42. });
  43. return false;
  44. }
  45. if (v.reason == 'normal' || v.reason == 'room_closed') {
  46. return false;
  47. }
  48. if (v.isManual) {
  49. return false;
  50. }
  51. return true;
  52. }
  53. private _isReconnecting = false;
  54. private checkNetwork(): void {
  55. if (this.isInGame) {
  56. if (gameNet.conn && !this._isReconnecting && !gameNet.conn.isConnected) {
  57. if (this.needReconnect(gameNet.lastDisconnectReason)) {
  58. this._isReconnecting = true;
  59. this.enabled = false;
  60. this.tryReconnect(gameNet);
  61. }
  62. }
  63. }
  64. else {
  65. if(lobbyNet.type != 'http'){
  66. let net = lobbyNet as NetLobbyServer;
  67. if (net.conn && !this._isReconnecting && !net.conn.isConnected) {
  68. if (this.needReconnect(net.lastDisconnectReason)) {
  69. this._isReconnecting = true;
  70. this.enabled = false;
  71. this.tryReconnect(net);
  72. }
  73. }
  74. }
  75. }
  76. }
  77. private async tryReconnect(net: WebsocketClient) {
  78. this._tryTimes++;
  79. if (this._tryTimes > 10) {
  80. tgx.UIWaiting.hide();
  81. this.enabled = false;
  82. tgx.UIAlert.show('Please check the network and login again!').onClick(() => {
  83. tgx.SceneUtil.loadScene(SceneDef.LOGIN);
  84. });
  85. return;
  86. }
  87. let isSucc = false;
  88. if (this.isInGame) {
  89. if (net == gameNet) {
  90. console.log('game reconnecting.');
  91. tgx.UIWaiting.show('Reconnecting...');
  92. let ret = await GameSceneUtil.inst.enterGame(gameNet.authParams);
  93. if (ret.isSucc) {
  94. isSucc = true;
  95. }
  96. else if (ret.err.code == 'ROOM_NOT_EXISTS') {
  97. RoomMgr.inst.backToLobby();
  98. return;
  99. }
  100. }
  101. }
  102. else {
  103. console.log('lobby reconnecting.');
  104. tgx.UIWaiting.show('Reconnecting...');
  105. let ret = await net.ensureConnected();
  106. isSucc = ret.isSucc;
  107. /**
  108. * @en if already login success, need to verify again
  109. * @zh 如果已经登录成功,则需要再次验证
  110. */
  111. if (ret.isSucc) {
  112. let ret1 = await LobbyMgr.inst.doAuth();
  113. if (!ret1.isSucc) {
  114. tgx.UIWaiting.hide();
  115. this.enabled = false;
  116. tgx.UIAlert.show("请检查网络,重新登录!").onClick(bOK => {
  117. tgx.SceneUtil.loadScene(SceneDef.LOGIN);
  118. });
  119. return;
  120. }
  121. }
  122. }
  123. /**
  124. * @en if re-connect failed, try again after 3s
  125. * @zh 如果没有重连成功,则一定时间后再次尝试
  126. */
  127. if (!isSucc) {
  128. setTimeout(() => {
  129. this.tryReconnect(net);
  130. }, 3000);
  131. }
  132. }
  133. }