From 2cd64981a18c33940de33f038ab3e5bdf8924565 Mon Sep 17 00:00:00 2001 From: luke Date: Mon, 23 Jun 2025 16:28:06 +0800 Subject: [PATCH] =?UTF-8?q?fix=20SSE=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../infrastructure/util/http/SseOutputAdapter.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/knowledge/base/infrastructure/util/http/SseOutputAdapter.java b/src/main/java/com/knowledge/base/infrastructure/util/http/SseOutputAdapter.java index 7f91c9d..2bda21f 100644 --- a/src/main/java/com/knowledge/base/infrastructure/util/http/SseOutputAdapter.java +++ b/src/main/java/com/knowledge/base/infrastructure/util/http/SseOutputAdapter.java @@ -1,6 +1,5 @@ package com.knowledge.base.infrastructure.util.http; -import cn.hutool.core.util.StrUtil; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import java.io.IOException; @@ -15,8 +14,16 @@ public class SseOutputAdapter implements WriterAdapter { @Override public void writeLine(String line) throws IOException { - if (StrUtil.isNotBlank(line)) { - emitter.send(SseEmitter.event().data(line)); + if (line == null || line.isBlank()) { + return; } + + // 如果 line 以 "data: " 开头,则去除该前缀 + if (line.startsWith("data: ")) { + line = line.substring(6).trim(); + } + + // 发送单行 SSE 事件 + emitter.send(SseEmitter.event().data(line)); } }