fix: 暂存
This commit is contained in:
@@ -4,7 +4,6 @@ import cn.dev33.satoken.stp.StpUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -12,11 +11,12 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
/**
|
||||
* SSE 订阅端点
|
||||
*
|
||||
* 提供 GET /sse/subscribe 端点建立持久化 SSE 连接。
|
||||
* 心跳由 SseConnectionManager 统一批量管理,此处不再维护 per-connection 定时器。
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@@ -24,11 +24,10 @@ import java.util.concurrent.ScheduledFuture;
|
||||
@RequiredArgsConstructor
|
||||
public class SseController {
|
||||
|
||||
/** SseEmitter 超时时间(30 分钟),超时后由 SseConnectionManager 的 onTimeout 回调清理 */
|
||||
private static final long SSE_TIMEOUT_MS = 30 * 60 * 1000L;
|
||||
private static final Duration HEARTBEAT_INTERVAL = Duration.ofSeconds(30);
|
||||
|
||||
private final SseConnectionManager connectionManager;
|
||||
private final TaskScheduler sseTaskScheduler;
|
||||
|
||||
@GetMapping(value = "/subscribe", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public SseEmitter subscribe(
|
||||
@@ -37,21 +36,7 @@ public class SseController {
|
||||
SseEmitter emitter = new SseEmitter(SSE_TIMEOUT_MS);
|
||||
String connectionId = connectionManager.register(userId, emitter);
|
||||
|
||||
ScheduledFuture<?> heartbeatTask = sseTaskScheduler.scheduleAtFixedRate(() -> {
|
||||
try {
|
||||
emitter.send(SseEmitter.event().name("heartbeat").data(""));
|
||||
} catch (IOException | IllegalStateException e) {
|
||||
log.debug("[SSE] 心跳发送失败, userId={}, connectionId={}, reason={}",
|
||||
userId, connectionId, e.getMessage());
|
||||
connectionManager.remove(userId, connectionId);
|
||||
}
|
||||
}, HEARTBEAT_INTERVAL);
|
||||
|
||||
Runnable cancelHeartbeat = () -> heartbeatTask.cancel(false);
|
||||
emitter.onCompletion(cancelHeartbeat);
|
||||
emitter.onTimeout(cancelHeartbeat);
|
||||
emitter.onError(error -> cancelHeartbeat.run());
|
||||
|
||||
// 发送初始连接确认
|
||||
try {
|
||||
emitter.send(SseEmitter.event()
|
||||
.name("connected")
|
||||
|
||||
Reference in New Issue
Block a user