diff --git a/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/core/SseEmitterManager.java b/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/core/SseEmitterManager.java index 289a6ed97..e00c5930f 100644 --- a/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/core/SseEmitterManager.java +++ b/ruoyi-common/ruoyi-common-sse/src/main/java/org/dromara/common/sse/core/SseEmitterManager.java @@ -116,24 +116,19 @@ public class SseEmitterManager { * SSE心跳检测,关闭无效连接 */ public void sseMonitor() { - log.info("开始执行 SSE 心跳检测"); - for (Map.Entry> userEmitters : USER_TOKEN_EMITTERS.entrySet()) { - Map emitters = userEmitters.getValue(); - if (MapUtil.isNotEmpty(emitters)) { - for (Map.Entry entry : emitters.entrySet()) { - try { - // 向客户端发送心跳 - entry.getValue().send(SseEmitter.event().comment("heartbeat")); - } catch (Exception e) { - log.warn("心跳发送失败, 关闭连接: userId={}, token={}", userEmitters.getKey(), entry.getKey()); - SseEmitter remove = emitters.remove(entry.getKey()); - if (remove != null) { - remove.complete(); - } - } + log.info("开始 SSE 心跳"); + USER_TOKEN_EMITTERS.forEach((userId, map) -> + map.entrySet().removeIf(e -> { + try { + e.getValue().send(SseEmitter.event().comment("heartbeat")); + return false; + } catch (Exception ex) { + log.warn("心跳失败,移除连接: userId={}, token={}", userId, e.getKey()); + e.getValue().complete(); + return true; } - } - } + }) + ); } /**