2026-06-22 13:43:25 +08:00
|
|
|
package com.webgame.webgamebackend.sse;
|
|
|
|
|
|
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.http.MediaType;
|
2026-06-24 08:30:22 +08:00
|
|
|
import org.springframework.scheduling.TaskScheduler;
|
2026-06-22 13:43:25 +08:00
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
2026-06-24 08:30:22 +08:00
|
|
|
import org.springframework.web.bind.annotation.RequestHeader;
|
2026-06-22 13:43:25 +08:00
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2026-06-24 08:30:22 +08:00
|
|
|
import java.time.Duration;
|
|
|
|
|
import java.util.concurrent.ScheduledFuture;
|
2026-06-22 13:43:25 +08:00
|
|
|
|
|
|
|
|
/**
|
2026-06-24 08:30:22 +08:00
|
|
|
* SSE 订阅端点
|
2026-06-22 13:43:25 +08:00
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/sse")
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class SseController {
|
|
|
|
|
|
2026-06-24 08:30:22 +08:00
|
|
|
private static final long SSE_TIMEOUT_MS = 30 * 60 * 1000L;
|
|
|
|
|
private static final Duration HEARTBEAT_INTERVAL = Duration.ofSeconds(30);
|
|
|
|
|
|
2026-06-22 13:43:25 +08:00
|
|
|
private final SseConnectionManager connectionManager;
|
2026-06-24 08:30:22 +08:00
|
|
|
private final TaskScheduler sseTaskScheduler;
|
2026-06-22 13:43:25 +08:00
|
|
|
|
|
|
|
|
@GetMapping(value = "/subscribe", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
2026-06-24 08:30:22 +08:00
|
|
|
public SseEmitter subscribe(
|
|
|
|
|
@RequestHeader(value = "Last-Event-ID", required = false) String lastEventId) {
|
2026-06-22 13:43:25 +08:00
|
|
|
String userId = StpUtil.getLoginIdAsString();
|
2026-06-24 08:30:22 +08:00
|
|
|
SseEmitter emitter = new SseEmitter(SSE_TIMEOUT_MS);
|
|
|
|
|
String connectionId = connectionManager.register(userId, emitter);
|
2026-06-22 13:43:25 +08:00
|
|
|
|
2026-06-24 08:30:22 +08:00
|
|
|
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());
|
2026-06-22 13:43:25 +08:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
emitter.send(SseEmitter.event()
|
|
|
|
|
.name("connected")
|
|
|
|
|
.data("{\"message\":\"连接成功\"}"));
|
2026-06-24 08:30:22 +08:00
|
|
|
} catch (IOException | IllegalStateException e) {
|
|
|
|
|
log.warn("[SSE] 发送 connected 事件失败, userId={}, connectionId={}",
|
|
|
|
|
userId, connectionId, e);
|
|
|
|
|
connectionManager.remove(userId, connectionId);
|
2026-06-22 13:43:25 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-24 08:30:22 +08:00
|
|
|
if (lastEventId != null && !lastEventId.isBlank()) {
|
|
|
|
|
log.debug("[SSE] 客户端重连, Last-Event-ID={}, userId={}, connectionId={}",
|
|
|
|
|
lastEventId, userId, connectionId);
|
|
|
|
|
}
|
2026-06-22 13:43:25 +08:00
|
|
|
|
2026-06-24 08:30:22 +08:00
|
|
|
log.info("[SSE] 连接建立成功, userId={}, connectionId={}, onlineConnections={}",
|
|
|
|
|
userId, connectionId, connectionManager.getOnlineCount());
|
2026-06-22 13:43:25 +08:00
|
|
|
return emitter;
|
|
|
|
|
}
|
|
|
|
|
}
|