feat(websocket): integrate WebSocket support and refactor SSE handling
All checks were successful
Build Backend / build (push) Successful in 18m57s
Build Backend / build-docker (push) Successful in 1m4s

- Added WebSocket support by introducing a new WSHandler and related infrastructure.
- Replaced SSE implementations with WebSocket equivalents across message and QR code handlers.
- Updated service and repository layers to utilize WebSocket for real-time messaging.
- Removed obsolete SSE hub and related code, streamlining the application for WebSocket usage.
- Enhanced router to include WebSocket endpoints for real-time communication.
This commit is contained in:
lafay
2026-03-26 21:17:49 +08:00
parent c6848aba06
commit 6d335e393d
19 changed files with 1277 additions and 315 deletions

View File

@@ -8,7 +8,7 @@ import (
"github.com/gin-gonic/gin"
"carrot_bbs/internal/pkg/response"
"carrot_bbs/internal/pkg/sse"
"carrot_bbs/internal/pkg/ws"
"carrot_bbs/internal/service"
)
@@ -44,16 +44,16 @@ func (h *QRCodeHandler) GetQRCode(c *gin.Context) {
})
}
// SSEEvents SSE事件流
// WSEvents WebSocket事件流
// GET /api/v1/auth/qrcode/events?session_id=xxx
func (h *QRCodeHandler) SSEEvents(c *gin.Context) {
func (h *QRCodeHandler) WSEvents(c *gin.Context) {
sessionID := c.Query("session_id")
if sessionID == "" {
response.BadRequest(c, "session_id is required")
return
}
ch, cancel, replay := h.qrcodeService.GetSSEHub().Subscribe(sessionID, 0)
ch, cancel, replay := h.qrcodeService.GetWSHub().Subscribe(sessionID, 0)
defer cancel()
w := c.Writer
@@ -70,8 +70,8 @@ func (h *QRCodeHandler) SSEEvents(c *gin.Context) {
c.Status(http.StatusOK)
flusher.Flush()
writeEvent := func(ev sse.Event) bool {
data, err := sse.EncodeData(ev)
writeEvent := func(ev ws.Event) bool {
data, err := ws.EncodeData(ev)
if err != nil {
return false
}