From f2d2631ce9e54600bf3338cc6b322126e5cfe3b2 Mon Sep 17 00:00:00 2001 From: jlzhou <12020042@qq.com> Date: Wed, 8 Jan 2025 16:29:23 +0800 Subject: [PATCH] =?UTF-8?q?update=20SSE=E6=94=AF=E6=8C=81=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=A0=B9=E6=8D=AE=E6=9D=A1=E4=BB=B6=E5=8F=91=E9=80=81?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/sse/SseApiSupport.java | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/sse/SseApiSupport.java b/ruoyi-common/src/main/java/com/ruoyi/common/sse/SseApiSupport.java index 434d433..399a6ff 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/sse/SseApiSupport.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/sse/SseApiSupport.java @@ -3,10 +3,10 @@ package com.ruoyi.common.sse; import cn.hutool.core.collection.ListUtil; import cn.hutool.core.util.ObjUtil; import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.PathVariable; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.function.Predicate; public abstract class SseApiSupport { protected final List> emitters = new CopyOnWriteArrayList<>(); @@ -45,15 +45,14 @@ public abstract class SseApiSupport { } /** - * 发送某种类型的action消息 - * - * @param type 消息类型 + * 根据条件发送action消息 + * @param predicate 条件为真发送 * @param action action消息 */ - public final void send(T type, SseAction action) { + public final void send(Predicate> predicate, SseAction action) { List> removes = ListUtil.list(true); for (SseEmitter e : emitters) { - if (ObjUtil.isNull(type) || ObjUtil.isNull(e.getType()) || ObjUtil.equals(type, e.getType())) { + if (predicate.test(e)) { try { e.send(action, MediaType.APPLICATION_JSON); } catch (Exception e1) { @@ -64,6 +63,27 @@ public abstract class SseApiSupport { emitters.removeAll(removes); } + /** + * 根据条件发送action消息 + * @param predicate 条件为真发送 + * @param action action字符串 + * @param data 具体内容 + */ + public final void send(Predicate> predicate, String action, Object data) { + send(predicate, SseAction.builder().action(action).data(data).build()); + } + + /** + * 发送某种类型的action消息 + * + * @param type 消息类型 + * @param action action消息 + */ + public final void send(T type, SseAction action) { + send(e -> ObjUtil.isNull(type) || ObjUtil.isNull(e.getType()) || ObjUtil.equals(type, e.getType()), action); + + } + /** * 发送某种类型的action消息 * @@ -91,7 +111,7 @@ public abstract class SseApiSupport { * @param action */ public final void sendAll(SseAction action) { - send(null, action); + send((T)null, action); }