fix(ecom): 导入任务 SSE 通知相关小修复

This commit is contained in:
小蟑螂
2026-05-06 03:54:09 +08:00
parent b7e4b6a77b
commit 955b29c750
3 changed files with 13 additions and 8 deletions
@@ -200,9 +200,10 @@ public class SseNotificationServiceImpl implements SseNotificationService {
List<EmitterEntry> entries = userConnections.get(userId);
if (entries == null || entries.isEmpty()) return;
// initialDelay=0:连接建立后尽快发首帧 ping,避免客户端在「首 ping 前」用短窗口误判超时
ScheduledFuture<?> task = scheduler.scheduleAtFixedRate(
() -> heartbeatForUser(userId),
HEARTBEAT_INTERVAL_SECONDS,
0,
HEARTBEAT_INTERVAL_SECONDS,
TimeUnit.SECONDS
);
@@ -46,10 +46,7 @@ class SseConnection {
new Map();
// EventSource 原生状态常量
private readyState: number = EventSource.CLOSED;
/** 设备标识,同一标签页内共用,重开标签页是新ID */
private deviceId: string;
/** 设备标识,每个标签页独立,不跨标签页共享 */
/** 每个标签页独立 deviceId,重开标签页为新 ID */
private deviceId: string;
constructor() {
@@ -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) {