From 9a2dd66df41ffdd9550b02ae959c316c4283423f Mon Sep 17 00:00:00 2001 From: Azure <983547216@qq.com> Date: Mon, 22 Jun 2026 14:09:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20SSE=20=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=8E=A5=E5=8F=A3=EF=BC=88=E5=9C=A8=E7=BA=BF=E4=BA=BA?= =?UTF-8?q?=E6=95=B0=E6=9F=A5=E8=AF=A2=20+=20=E6=89=8B=E5=8A=A8=E5=B9=BF?= =?UTF-8?q?=E6=92=AD=E4=BA=8B=E4=BB=B6=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../webgamebackend/sse/SseController.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/main/java/com/webgame/webgamebackend/sse/SseController.java b/src/main/java/com/webgame/webgamebackend/sse/SseController.java index 7d07401..c267245 100644 --- a/src/main/java/com/webgame/webgamebackend/sse/SseController.java +++ b/src/main/java/com/webgame/webgamebackend/sse/SseController.java @@ -1,15 +1,21 @@ package com.webgame.webgamebackend.sse; import cn.dev33.satoken.stp.StpUtil; +import com.alibaba.fastjson2.JSON; +import com.webgame.webgamebackend.common.event.SseEvent; +import com.webgame.webgamebackend.common.event.SseEventType; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; 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; +import java.util.Map; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -76,4 +82,44 @@ public class SseController { log.info("[SSE] 新连接建立, userId={}, 当前在线={}", userId, connectionManager.getOnlineCount()); return emitter; } + + // ==================== 测试接口 ==================== + + /** + * 查看当前 SSE 在线连接数 + * + * GET /sse/test/online + */ + @GetMapping("/test/online") + public Map onlineCount() { + return Map.of("onlineCount", connectionManager.getOnlineCount()); + } + + /** + * 手动发送广播事件(测试用) + * + * POST /sse/test/broadcast + * Body: { "event": "room_created", "data": { "roomId": "test", ... } } + * + * event 可选值: room_created / room_updated / room_removed / kicked / game_started / balance_chg + */ + @PostMapping("/test/broadcast") + public Map testBroadcast(@RequestBody Map body) { + String eventType = (String) body.get("event"); + Object data = body.get("data"); + + if (eventType == null || data == null) { + return Map.of("success", false, "error", "event 和 data 字段为必填"); + } + + try { + SseEventType type = SseEventType.valueOf(eventType.toUpperCase()); + SseEvent event = SseEvent.broadcast(type, JSON.toJSONString(data)); + connectionManager.broadcast(event); + log.info("[SSE] 测试广播, type={}, data={}", eventType, JSON.toJSONString(data)); + return Map.of("success", true, "eventType", eventType, "onlineCount", connectionManager.getOnlineCount()); + } catch (IllegalArgumentException e) { + return Map.of("success", false, "error", "无效的事件类型: " + eventType); + } + } }