update 增加sse注释说明

2.X
AprilWind 5 months ago
parent 190cf45ef1
commit be7a4d7a8f

@ -15,6 +15,11 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.util.List; import java.util.List;
/**
* SSE
*
* @author Lion Li
*/
@RestController @RestController
@ConditionalOnProperty(value = "sse.enabled", havingValue = "true") @ConditionalOnProperty(value = "sse.enabled", havingValue = "true")
@RequiredArgsConstructor @RequiredArgsConstructor
@ -22,6 +27,9 @@ public class SseController implements DisposableBean {
private final SseEmitterManager sseEmitterManager; private final SseEmitterManager sseEmitterManager;
/**
* SSE
*/
@GetMapping(value = "${sse.path}", produces = MediaType.TEXT_EVENT_STREAM_VALUE) @GetMapping(value = "${sse.path}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter connect() { public SseEmitter connect() {
String tokenValue = StpUtil.getTokenValue(); String tokenValue = StpUtil.getTokenValue();
@ -29,6 +37,9 @@ public class SseController implements DisposableBean {
return sseEmitterManager.connect(userId, tokenValue); return sseEmitterManager.connect(userId, tokenValue);
} }
/**
* SSE
*/
@GetMapping(value = "${sse.path}/close") @GetMapping(value = "${sse.path}/close")
public R<Void> close() { public R<Void> close() {
String tokenValue = StpUtil.getTokenValue(); String tokenValue = StpUtil.getTokenValue();
@ -37,6 +48,12 @@ public class SseController implements DisposableBean {
return R.ok(); return R.ok();
} }
/**
*
*
* @param userId ID
* @param msg
*/
@GetMapping(value = "${sse.path}/send") @GetMapping(value = "${sse.path}/send")
public R<Void> send(Long userId, String msg) { public R<Void> send(Long userId, String msg) {
SseMessageDto dto = new SseMessageDto(); SseMessageDto dto = new SseMessageDto();
@ -46,12 +63,20 @@ public class SseController implements DisposableBean {
return R.ok(); return R.ok();
} }
/**
*
*
* @param msg
*/
@GetMapping(value = "${sse.path}/sendAll") @GetMapping(value = "${sse.path}/sendAll")
public R<Void> send(String msg) { public R<Void> send(String msg) {
sseEmitterManager.publishAll(msg); sseEmitterManager.publishAll(msg);
return R.ok(); return R.ok();
} }
/**
*
*/
@Override @Override
public void destroy() throws Exception { public void destroy() throws Exception {
// 销毁时不需要做什么 此方法避免无用操作报错 // 销毁时不需要做什么 此方法避免无用操作报错

@ -13,8 +13,14 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer; import java.util.function.Consumer;
/**
* Server-Sent Events (SSE)
*
* @author Lion Li
*/
@Slf4j @Slf4j
public class SseEmitterManager { public class SseEmitterManager {
/** /**
* *
*/ */
@ -22,24 +28,44 @@ public class SseEmitterManager {
private final static Map<Long, Map<String, SseEmitter>> USER_TOKEN_EMITTERS = new ConcurrentHashMap<>(); private final static Map<Long, Map<String, SseEmitter>> USER_TOKEN_EMITTERS = new ConcurrentHashMap<>();
/**
* SSE
*
* @param userId
* @param token
* @return SseEmitter SSE
*/
public SseEmitter connect(Long userId, String token) { public SseEmitter connect(Long userId, String token) {
// 从 USER_TOKEN_EMITTERS 中获取或创建当前用户的 SseEmitter 映射表ConcurrentHashMap
// 每个用户可以有多个 SSE 连接,通过 token 进行区分
Map<String, SseEmitter> emitters = USER_TOKEN_EMITTERS.computeIfAbsent(userId, k -> new ConcurrentHashMap<>()); Map<String, SseEmitter> emitters = USER_TOKEN_EMITTERS.computeIfAbsent(userId, k -> new ConcurrentHashMap<>());
// 创建一个新的 SseEmitter 实例,超时时间设置为 0 表示无限制
SseEmitter emitter = new SseEmitter(0L); SseEmitter emitter = new SseEmitter(0L);
emitters.put(token, emitter); emitters.put(token, emitter);
// 当 emitter 完成、超时或发生错误时,从映射表中移除对应的 token
emitter.onCompletion(() -> emitters.remove(token)); emitter.onCompletion(() -> emitters.remove(token));
emitter.onTimeout(() -> emitters.remove(token)); emitter.onTimeout(() -> emitters.remove(token));
emitter.onError((e) -> emitters.remove(token)); emitter.onError((e) -> emitters.remove(token));
try { try {
// 向客户端发送一条连接成功的事件
emitter.send(SseEmitter.event().comment("connected")); emitter.send(SseEmitter.event().comment("connected"));
} catch (IOException e) { } catch (IOException e) {
// 如果发送消息失败,则从映射表中移除 emitter
emitters.remove(token); emitters.remove(token);
} }
return emitter; return emitter;
} }
/**
* SSE
*
* @param userId
* @param token
*/
public void disconnect(Long userId, String token) { public void disconnect(Long userId, String token) {
Map<String, SseEmitter> emitters = USER_TOKEN_EMITTERS.get(userId); Map<String, SseEmitter> emitters = USER_TOKEN_EMITTERS.get(userId);
if (emitters != null) { if (emitters != null) {

@ -8,7 +8,7 @@ import org.dromara.common.sse.core.SseEmitterManager;
import org.dromara.common.sse.dto.SseMessageDto; import org.dromara.common.sse.dto.SseMessageDto;
/** /**
* * SSE
* *
* @author Lion Li * @author Lion Li
*/ */

Loading…
Cancel
Save