mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 07:09:20 +08:00
fix(ecom): 导入任务 SSE 通知相关小修复
This commit is contained in:
@@ -175,6 +175,7 @@ export const useImportTaskStore = defineStore('import-task', () => {
|
||||
|
||||
// 监听连接成功事件
|
||||
const connectedHandler = (event: MessageEvent) => {
|
||||
lastPingTime.value = Date.now();
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
if (data.type === 'connected') {
|
||||
@@ -200,13 +201,17 @@ export const useImportTaskStore = defineStore('import-task', () => {
|
||||
|
||||
connection.connect();
|
||||
|
||||
// 启动心跳检测:每5秒检查一次,超过20秒没收到ping则断开重连
|
||||
// 心跳检测:须大于服务端首包 ping 间隔(后端约 15s 一发),避免「已连接但首 ping 未到」误判超时
|
||||
const HEARTBEAT_STALE_MS = 45_000;
|
||||
lastPingTime.value = Date.now();
|
||||
if (pingCheckTimer !== null) {
|
||||
clearInterval(pingCheckTimer);
|
||||
}
|
||||
pingCheckTimer = setInterval(() => {
|
||||
if (lastPingTime.value !== null && Date.now() - lastPingTime.value > 20_000) {
|
||||
if (
|
||||
lastPingTime.value !== null &&
|
||||
Date.now() - lastPingTime.value > HEARTBEAT_STALE_MS
|
||||
) {
|
||||
console.warn('[SSE] 心跳超时,断开并重连');
|
||||
scheduleReconnect(1000);
|
||||
}
|
||||
@@ -227,8 +232,9 @@ export const useImportTaskStore = defineStore('import-task', () => {
|
||||
lastPingTime.value = Date.now();
|
||||
});
|
||||
|
||||
// 接收任务进度更新
|
||||
// 接收任务进度更新(也算链路存活,刷新心跳时间)
|
||||
connection.addEventListener('task_progress', (event) => {
|
||||
lastPingTime.value = Date.now();
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
if (data.type === 'task_progress' && data.task) {
|
||||
@@ -241,6 +247,7 @@ export const useImportTaskStore = defineStore('import-task', () => {
|
||||
|
||||
// 所有任务完成
|
||||
connection.addEventListener('all_tasks_completed', () => {
|
||||
lastPingTime.value = Date.now();
|
||||
try {
|
||||
fetchTasks(true);
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user