mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-18 17:38:48 +08:00
update 优化 sse 心跳检测写法
This commit is contained in:
parent
9ba0b0ef69
commit
40e13ce136
@ -116,24 +116,19 @@ public class SseEmitterManager {
|
|||||||
* SSE心跳检测,关闭无效连接
|
* SSE心跳检测,关闭无效连接
|
||||||
*/
|
*/
|
||||||
public void sseMonitor() {
|
public void sseMonitor() {
|
||||||
log.info("开始执行 SSE 心跳检测");
|
log.info("开始 SSE 心跳");
|
||||||
for (Map.Entry<Long, Map<String, SseEmitter>> userEmitters : USER_TOKEN_EMITTERS.entrySet()) {
|
USER_TOKEN_EMITTERS.forEach((userId, map) ->
|
||||||
Map<String, SseEmitter> emitters = userEmitters.getValue();
|
map.entrySet().removeIf(e -> {
|
||||||
if (MapUtil.isNotEmpty(emitters)) {
|
try {
|
||||||
for (Map.Entry<String, SseEmitter> entry : emitters.entrySet()) {
|
e.getValue().send(SseEmitter.event().comment("heartbeat"));
|
||||||
try {
|
return false;
|
||||||
// 向客户端发送心跳
|
} catch (Exception ex) {
|
||||||
entry.getValue().send(SseEmitter.event().comment("heartbeat"));
|
log.warn("心跳失败,移除连接: userId={}, token={}", userId, e.getKey());
|
||||||
} catch (Exception e) {
|
e.getValue().complete();
|
||||||
log.warn("心跳发送失败, 关闭连接: userId={}, token={}", userEmitters.getKey(), entry.getKey());
|
return true;
|
||||||
SseEmitter remove = emitters.remove(entry.getKey());
|
|
||||||
if (remove != null) {
|
|
||||||
remove.complete();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user