feat: implement client heartbeat mechanism with 2-second interval

This commit is contained in:
Megghy
2025-10-03 22:55:15 +08:00
parent 4e93b371f4
commit fc465a8e0d
2 changed files with 31 additions and 6 deletions

View File

@@ -29,6 +29,35 @@ const accountInfo = useAccount()
export const clientInited = ref(false)
let tray: TrayIcon
let heartbeatTimer: number | null = null
async function sendHeartbeat() {
try {
await invoke('heartbeat')
} catch (error) {
console.error('发送心跳失败:', error)
}
}
function startHeartbeat() {
// 立即发送一次,确保后端在加载后快速收到心跳
void sendHeartbeat()
// 之后每 5 秒发送一次心跳(后端超时时间为 15 秒)
heartbeatTimer = window.setInterval(() => {
void sendHeartbeat()
}, 2000)
info('[心跳] 定时器已启动,间隔 2 秒')
}
function stopHeartbeat() {
if (heartbeatTimer !== null) {
clearInterval(heartbeatTimer)
heartbeatTimer = null
info('[心跳] 定时器已停止')
}
}
export async function initAll(isOnBoot: boolean) {
const setting = useSettings()
if (clientInited.value) {
@@ -150,6 +179,7 @@ export async function initAll(isOnBoot: boolean) {
useAutoAction().init()
useBiliFunction().init()
startHeartbeat()
clientInited.value = true
}
export function OnClientUnmounted() {
@@ -157,6 +187,7 @@ export function OnClientUnmounted() {
clientInited.value = false
}
stopHeartbeat()
tray.close()
// useDanmakuWindow().closeWindow();
}

6
src/components.d.ts vendored
View File

@@ -22,19 +22,13 @@ declare module 'vue' {
NAvatar: typeof import('naive-ui')['NAvatar']
NButton: typeof import('naive-ui')['NButton']
NCard: typeof import('naive-ui')['NCard']
NEllipsis: typeof import('naive-ui')['NEllipsis']
NEmpty: typeof import('naive-ui')['NEmpty']
NFlex: typeof import('naive-ui')['NFlex']
NFormItemGi: typeof import('naive-ui')['NFormItemGi']
NGridItem: typeof import('naive-ui')['NGridItem']
NIcon: typeof import('naive-ui')['NIcon']
NImage: typeof import('naive-ui')['NImage']
NPopconfirm: typeof import('naive-ui')['NPopconfirm']
NScrollbar: typeof import('naive-ui')['NScrollbar']
NSpace: typeof import('naive-ui')['NSpace']
NTag: typeof import('naive-ui')['NTag']
NText: typeof import('naive-ui')['NText']
NTime: typeof import('naive-ui')['NTime']
PointGoodsItem: typeof import('./components/manage/PointGoodsItem.vue')['default']
PointHistoryCard: typeof import('./components/manage/PointHistoryCard.vue')['default']
PointOrderCard: typeof import('./components/manage/PointOrderCard.vue')['default']