Jelajahi Sumber

心跳机制

lvjincheng 4 tahun lalu
induk
melakukan
2b27b03eb2
3 mengubah file dengan 69 tambahan dan 0 penghapusan
  1. 19 0
      Assets/JC/JCEngine.cs
  2. 39 0
      Assets/JC/Untiy/JCCoroutine.cs
  3. 11 0
      Assets/JC/Untiy/JCCoroutine.cs.meta

+ 19 - 0
Assets/JC/JCEngine.cs

@@ -1,8 +1,11 @@
 using System;
+using System.Collections;
 using System.Collections.Generic;
 using BestHTTP.WebSocket;
 using Newtonsoft.Json;
 using Newtonsoft.Json.Linq;
+using JC.Unity;
+using UnityEngine;
 
 namespace JCEngineCore {
     public class JCEngine {
@@ -65,6 +68,7 @@ namespace JCEngineCore {
     class WebSocketServer {
         private WebSocket webSocket;
         private JCEntity tempEntity;
+        private Coroutine heartBeatCoroutine;  
         public WebSocketServer(string url, JCEntity entity) {
             this.webSocket = new WebSocket(new Uri(url));
             this.webSocket.StartPingThread = true;
@@ -141,8 +145,15 @@ namespace JCEngineCore {
                 }
             } catch (Exception) {}
             this.tempEntity.loaded = true;
+            if (heartBeatCoroutine == null) {
+                heartBeatCoroutine = CoroutineStarter.Start(doHeartBeat());
+            }
         }
         public void destroyTempEntity() {
+            if (heartBeatCoroutine != null) {
+                CoroutineStarter.Stop(heartBeatCoroutine);
+                heartBeatCoroutine = null;
+            }
             if (this.tempEntity.isValid) {
                 this.tempEntity.isValid = false;
                 this.tempEntity.onDestroy();            
@@ -150,6 +161,14 @@ namespace JCEngineCore {
                 this.tempEntity.onMiss();
             }
         }
+        IEnumerator doHeartBeat() {
+            while (true) {
+                try {
+                    call("doHeartBeat");
+                } catch (System.Exception) {}
+                yield return new WaitForSecondsRealtime(5f);
+            }
+        }
     }
     class CallbackHandler {
         private static int nextID = 0;

+ 39 - 0
Assets/JC/Untiy/JCCoroutine.cs

@@ -0,0 +1,39 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace JC.Unity {
+    // 协程启动器
+    public class CoroutineStarter : MonoBehaviour
+    {
+
+        private static CoroutineStarter _instance;
+
+        private static CoroutineStarter Instance {
+            get {
+                if ( _instance == null ) {
+                    GameObject obj = new GameObject("CoroutineStarter");
+                    obj.AddComponent<CoroutineStarter>();
+                }
+                return _instance;
+            }
+        }
+
+        void Awake() {
+            _instance = this;
+            DontDestroyOnLoad(gameObject);
+        }
+
+        /// <summary>
+        /// 开启协程
+        /// </summary>
+        /// <param name="enumerator"></param>
+        public static Coroutine Start(IEnumerator enumerator) {
+            return Instance.StartCoroutine(enumerator);
+        }
+
+        public static void Stop(Coroutine coroutine) {
+            Instance.StopCoroutine(coroutine);
+        }
+    }
+}

+ 11 - 0
Assets/JC/Untiy/JCCoroutine.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 012c171b30b8c414c834a45cf12930e5
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: