future 1 year ago
parent
commit
d47eac097b
3 changed files with 35 additions and 17 deletions
  1. 5 5
      assets/scene/main.scene
  2. 30 6
      assets/script/socket/gameSocket.ts
  3. 0 6
      assets/script/socket/msgManager.ts

+ 5 - 5
assets/scene/main.scene

@@ -105,7 +105,7 @@
     "_lpos": {
       "__type__": "cc.Vec3",
       "x": 540,
-      "y": 960,
+      "y": 960.0000000000002,
       "z": 0
     },
     "_lrot": {
@@ -192,7 +192,7 @@
     "_contentSize": {
       "__type__": "cc.Size",
       "width": 1080,
-      "height": 1920
+      "height": 1920.0000000000002
     },
     "_anchorPoint": {
       "__type__": "cc.Vec2",
@@ -290,7 +290,7 @@
     "_priority": 0,
     "_fov": 45,
     "_fovAxis": 0,
-    "_orthoHeight": 960,
+    "_orthoHeight": 960.0000000000001,
     "_near": 0,
     "_far": 1000,
     "_color": {
@@ -561,7 +561,7 @@
     "_contentSize": {
       "__type__": "cc.Size",
       "width": 1080,
-      "height": 1920
+      "height": 1920.0000000000002
     },
     "_anchorPoint": {
       "__type__": "cc.Vec2",
@@ -867,7 +867,7 @@
     "_contentSize": {
       "__type__": "cc.Size",
       "width": 1080,
-      "height": 1920
+      "height": 1920.0000000000002
     },
     "_anchorPoint": {
       "__type__": "cc.Vec2",

+ 30 - 6
assets/script/socket/gameSocket.ts

@@ -1,7 +1,5 @@
 import { _decorator, Component, Node } from 'cc';
-import { userDataManager } from '../manager/userDataManager';
 import { msgManager } from './msgManager';
-import { config } from '../config';
 const { ccclass, property } = _decorator;
 
 @ccclass('gameSocket')
@@ -13,16 +11,22 @@ export class gameSocket extends Component {
         return gameSocket._instance;
     }
     sock:WebSocket = null;
+    private current_url:string = ''  //当前url
     is_connected:Boolean = false;
     stopConnected:Boolean = false;
     check_heartbeat_succeed:boolean = false;
     is_online_status:boolean = false; //是否是在线状态
 
+    private reconnectInterval = 1000;   // 重连间隔时间(毫秒)
+    private reconnectMaxAttempts = 30;  // 最大重连尝试次数
+    private reconnectAttempts = 0;      // 当前重连尝试次数
+
     private static RECV_HEART_BEAT ='1' //收到服务器心跳结果
     private static RECV_CHEAK_HEART_BEAT = '2' //服务器查询我的心跳
 
     _on_opened(event:Event):void {
         console.log('_on_opened=',event)
+        this.reconnectAttempts = 0
         if(this.is_connected===true){
 
         }else{
@@ -65,16 +69,35 @@ export class gameSocket extends Component {
         }
     }
 
-    _on_socket_close(event:any):void {
+    _on_socket_close(event:any):void {        
         console.log("_on_socket_close",event)
-        this.is_connected = false;
-        setTimeout(()=>{ msgManager.reConnect() },1000)
+        this.reConnectOperation()
+        this.is_connected = false;        
     }
 
     _on_socket_err(event:any):void {
         console.log("_on_socket_err",event)
+        this.reConnectOperation()
         this.is_connected = false;
-      //  this.close()
+    }
+
+    private reConnectOperation() {
+        if (this.reconnectAttempts < this.reconnectMaxAttempts) {
+            console.log('正在尝试重连...');
+            setTimeout(()=>{
+                this.reonnect()
+            },this.reconnectInterval)
+            this.reconnectAttempts++;
+        } else {
+            console.log('重连尝试次数已达上限');
+        }
+    }
+
+    reonnect() {
+        if(this.current_url.length<=0) {
+            return
+        } 
+        this.connect(this.current_url)
     }
 
     connect(url:any):void {
@@ -82,6 +105,7 @@ export class gameSocket extends Component {
             console.log("客户端退出链接,暂不提供重连")
             return
         }
+        this.current_url = url
         if(!this.is_connected){
             try{
                 this.sock = new WebSocket(url);

+ 0 - 6
assets/script/socket/msgManager.ts

@@ -3,7 +3,6 @@ import { gameSocket } from './gameSocket';
 import { config } from '../config';
 import { userDataManager } from '../manager/userDataManager';
 import { ClientEvent } from '../lib/clientEvent';
-import { GameManager } from '../GameManager';
 const { ccclass, property } = _decorator;
 
 @ccclass('msgManager')
@@ -16,11 +15,6 @@ export class msgManager extends Component {
         gameSocket.Instance.send_cmd(str)
     }
 
-    // 重新连接
-    public static reConnect(){
-        GameManager.openWebScoket()
-    }
-
     // 接受消息
     public static recv(json_data){
         console.log("接收消息",json_data)