|
|
|
|
@ -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<T> {
|
|
|
|
|
protected final List<SseEmitter<T>> emitters = new CopyOnWriteArrayList<>();
|
|
|
|
|
@ -45,15 +45,14 @@ public abstract class SseApiSupport<T> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送某种类型的action消息
|
|
|
|
|
*
|
|
|
|
|
* @param type 消息类型
|
|
|
|
|
* 根据条件发送action消息
|
|
|
|
|
* @param predicate 条件为真发送
|
|
|
|
|
* @param action action消息
|
|
|
|
|
*/
|
|
|
|
|
public final void send(T type, SseAction<?> action) {
|
|
|
|
|
public final void send(Predicate<SseEmitter<T>> predicate, SseAction<?> action) {
|
|
|
|
|
List<SseEmitter<T>> removes = ListUtil.list(true);
|
|
|
|
|
for (SseEmitter<T> 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<T> {
|
|
|
|
|
emitters.removeAll(removes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据条件发送action消息
|
|
|
|
|
* @param predicate 条件为真发送
|
|
|
|
|
* @param action action字符串
|
|
|
|
|
* @param data 具体内容
|
|
|
|
|
*/
|
|
|
|
|
public final void send(Predicate<SseEmitter<T>> 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<T> {
|
|
|
|
|
* @param action
|
|
|
|
|
*/
|
|
|
|
|
public final void sendAll(SseAction<?> action) {
|
|
|
|
|
send(null, action);
|
|
|
|
|
send((T)null, action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|