mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 01:49:19 +08:00
fix: 保持SSE长连接不断开,移除主动closeConnection逻辑
前端修复: - init()无条件建立SSE连接 - error handler无条件重连,去掉hasActiveTasks判断 后端修复: - notifyAllTasksCompleted不再延迟关闭连接 - SSE保持长连接持续接收任务状态更新
This commit is contained in:
+2
-12
@@ -188,27 +188,17 @@ public class SseNotificationServiceImpl implements SseNotificationService {
|
|||||||
try {
|
try {
|
||||||
Map<String, Object> completeMessage = new java.util.HashMap<>();
|
Map<String, Object> completeMessage = new java.util.HashMap<>();
|
||||||
completeMessage.put("type", "all_tasks_completed");
|
completeMessage.put("type", "all_tasks_completed");
|
||||||
completeMessage.put("message", "所有任务已完成,可以关闭连接");
|
completeMessage.put("message", "所有任务已完成");
|
||||||
|
|
||||||
emitter.send(SseEmitter.event()
|
emitter.send(SseEmitter.event()
|
||||||
.name("all_tasks_completed")
|
.name("all_tasks_completed")
|
||||||
.data(objectMapper.writeValueAsString(completeMessage)));
|
.data(objectMapper.writeValueAsString(completeMessage)));
|
||||||
|
|
||||||
logger.info("通知所有任务已完成,userId: {}", userId);
|
logger.info("通知所有任务已完成,userId: {}", userId);
|
||||||
|
// 不再主动关闭连接,让SSE保持长连接,前端持续接收任务更新
|
||||||
// 延迟关闭连接,给前端时间处理
|
|
||||||
new Thread(() -> {
|
|
||||||
try {
|
|
||||||
Thread.sleep(1000); // 等待1秒
|
|
||||||
closeConnection(userId);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error("通知所有任务完成失败,userId: {}", userId, e);
|
logger.error("通知所有任务完成失败,userId: {}", userId, e);
|
||||||
closeConnection(userId);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -203,13 +203,10 @@ export const useImportTaskStore = defineStore('import-task', () => {
|
|||||||
connection.addEventListener('error', (event) => {
|
connection.addEventListener('error', (event) => {
|
||||||
console.error('SSE连接错误:', event);
|
console.error('SSE连接错误:', event);
|
||||||
sseConnected.value = false;
|
sseConnected.value = false;
|
||||||
// 如果连接关闭,尝试重连
|
// 如果连接关闭,无条件重连,不判断活跃任务
|
||||||
if (connection.getReadyState() === EventSource.CLOSED) {
|
if (connection.getReadyState() === EventSource.CLOSED) {
|
||||||
// 延迟重连,避免频繁重连
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (hasActiveTasks.value) {
|
connectSse();
|
||||||
connectSse();
|
|
||||||
}
|
|
||||||
}, 5000);
|
}, 5000);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -238,10 +235,8 @@ export const useImportTaskStore = defineStore('import-task', () => {
|
|||||||
async function init() {
|
async function init() {
|
||||||
await fetchTasks(true);
|
await fetchTasks(true);
|
||||||
|
|
||||||
// 如果有进行中的任务,建立SSE连接
|
// 页面加载时无条件建立SSE连接,接收实时任务状态更新
|
||||||
if (hasActiveTasks.value && !sseConnection.value) {
|
connectSse();
|
||||||
connectSse();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
-15
@@ -960,21 +960,6 @@ const handleCreateTask = async () => {
|
|||||||
try {
|
try {
|
||||||
loading.value = true;
|
loading.value = true;
|
||||||
|
|
||||||
// 确保SSE连接已建立(如果还没有连接,先建立连接)
|
|
||||||
if (!importTaskStore.sseConnection || !importTaskStore.sseConnected) {
|
|
||||||
message.loading('正在建立连接...', 0);
|
|
||||||
try {
|
|
||||||
await importTaskStore.connectSse();
|
|
||||||
message.destroy();
|
|
||||||
} catch (error: any) {
|
|
||||||
message.destroy();
|
|
||||||
message.error(
|
|
||||||
error?.message || '建立SSE连接失败,无法实时接收任务进度',
|
|
||||||
);
|
|
||||||
// 即使连接失败,也允许继续创建任务
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 先上传文件到 OSS(使用统一文件系统)
|
// 先上传文件到 OSS(使用统一文件系统)
|
||||||
message.loading('正在上传文件...', 0);
|
message.loading('正在上传文件...', 0);
|
||||||
const fileInfo = await uploadFileToOss(selectedFile.value, {
|
const fileInfo = await uploadFileToOss(selectedFile.value, {
|
||||||
|
|||||||
Reference in New Issue
Block a user