瀏覽代碼

断线重连优化

lvjincheng 4 年之前
父節點
當前提交
298727912a
共有 1 個文件被更改,包括 9 次插入8 次删除
  1. 9 8
      Assets/BowArrow/Scripts/Network/UserPlayer.cs

+ 9 - 8
Assets/BowArrow/Scripts/Network/UserPlayer.cs

@@ -8,17 +8,19 @@ using DG.Tweening;
 public class UserPlayer : JCEntity
 {
     public static UserPlayer ins;
-    GameObject userPlayerGameObject;
     public UserPlayer() {
         ins = this;
-        userPlayerGameObject = new GameObject("UserPlayerGameObject");
-        GameObject.DontDestroyOnLoad(userPlayerGameObject);
     }
     public static void ConnectServer() {
         if (ins != null) return;
         if (HomeMgr.ins) HomeMgr.ins.ShowAuthLoginMask(true);
         JCEngine.boot(CommonConfig.businessServerWsURI, new UserPlayer());
     }
+    //之所以做成协程延迟触发,是因为用编辑器调试时,停止运行后会触发断线重连,就会造成游戏停止调试了,但socket还连接的现象。
+    IEnumerator ReconnenctServer() {
+        yield return new WaitForSecondsRealtime(0.1f);
+        JCEngine.reboot(this);
+    }
 
     bool canReconnnect = true;
 
@@ -34,22 +36,21 @@ public class UserPlayer : JCEntity
 
     public override void onDestroy() {
         Debug.Log("UserPlayer onDestroy()");
-        if (canReconnnect && userPlayerGameObject) {
-            JCEngine.reboot(this);
+        if (canReconnnect) {
+            JC.Unity.CoroutineStarter.Start(ReconnenctServer());
         }
     }
 
     public override void onMiss() {
         Debug.Log("UserPlayer onMiss()");
-        if (canReconnnect && userPlayerGameObject) {
-            JCEngine.reboot(this);
+        if (canReconnnect) {
+            JC.Unity.CoroutineStarter.Start(ReconnenctServer());
         }
     }
 
     public void Close() {
         if (ins == this) ins = null;
         this.canReconnnect = false;
-        if (userPlayerGameObject) GameObject.Destroy(userPlayerGameObject);
         this.channel.close();
         Debug.Log("user player close");
     }