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); }