lsc hace 2 semanas
padre
commit
bc45ac0dfa
Se han modificado 1 ficheros con 19 adiciones y 0 borrados
  1. 19 0
      src/views/Student/index.vue

+ 19 - 0
src/views/Student/index.vue

@@ -716,6 +716,7 @@ const connectionStatus = ref<'disconnected' | 'connecting' | 'connected'>('disco
 // 认证 token 相关变量
 const authToken = ref<string | null>(null)
 const authTokenUpdateTimer = ref<NodeJS.Timeout | null>(null)
+const heartbeatTimer = ref<NodeJS.Timeout | null>(null)
 const socketCheckTimer = ref<NodeJS.Timeout | null>(null)
 
 // 同步数据最大保留时间(40分钟)
@@ -4115,6 +4116,12 @@ onUnmounted(() => {
     authTokenUpdateTimer.value = null
   }
 
+  // 清理心跳定时器
+  if (heartbeatTimer.value) {
+    clearInterval(heartbeatTimer.value)
+    heartbeatTimer.value = null
+  }
+
   // 清理 socket 连接检查定时器
   if (socketCheckTimer.value) {
     clearInterval(socketCheckTimer.value)
@@ -4301,6 +4308,18 @@ const createWebSocketConnection = async (type = 1) => {
     )
     providerSocket.value.awareness.setLocalState(null)
     providerSocket.value.awareness.destroy() // 完全禁用 awareness
+    // ★ 心跳(用 providerSocket.value,断开时清除避免泄漏)
+    if (heartbeatTimer.value) clearInterval(heartbeatTimer.value)
+    heartbeatTimer.value = setInterval(() => {
+      if (providerSocket.value?.ws?.readyState === WebSocket.OPEN) {
+        try {
+          providerSocket.value.ws.send(new Uint8Array([0, 0]))
+        }
+        catch (e) {
+          console.error('heartbeatTimer', e)
+        }
+      }
+    }, 30000)
     // 启动定期更新 token
     // 30分钟后再次更新
     if (authTokenUpdateTimer.value) {