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

@@ -14,8 +14,8 @@ import (
apperrors "carrot_bbs/internal/errors"
"carrot_bbs/internal/model"
"carrot_bbs/internal/pkg/cursor"
"carrot_bbs/internal/pkg/sse"
"carrot_bbs/internal/pkg/utils"
"carrot_bbs/internal/pkg/ws"
"carrot_bbs/internal/repository"
"go.uber.org/zap"
@@ -111,19 +111,19 @@ type groupService struct {
messageRepo repository.MessageRepository
requestRepo repository.GroupJoinRequestRepository
notifyRepo repository.SystemNotificationRepository
sseHub *sse.Hub
wsHub *ws.Hub
cache cache.Cache
}
// NewGroupService 创建群组服务
func NewGroupService(groupRepo repository.GroupRepository, userRepo repository.UserRepository, messageRepo repository.MessageRepository, requestRepo repository.GroupJoinRequestRepository, notifyRepo repository.SystemNotificationRepository, sseHub *sse.Hub, cacheBackend cache.Cache) GroupService {
func NewGroupService(groupRepo repository.GroupRepository, userRepo repository.UserRepository, messageRepo repository.MessageRepository, requestRepo repository.GroupJoinRequestRepository, notifyRepo repository.SystemNotificationRepository, wsHub *ws.Hub, cacheBackend cache.Cache) GroupService {
return &groupService{
groupRepo: groupRepo,
userRepo: userRepo,
messageRepo: messageRepo,
requestRepo: requestRepo,
notifyRepo: notifyRepo,
sseHub: sseHub,
wsHub: wsHub,
cache: cacheBackend,
}
}
@@ -152,9 +152,9 @@ func (s *groupService) publishGroupNotice(groupID string, notice groupNoticeMess
)
return
}
if s.sseHub != nil {
if s.wsHub != nil {
for _, m := range members {
s.sseHub.PublishToUser(m.UserID, "group_notice", notice)
s.wsHub.PublishToUser(m.UserID, "group_notice", notice)
}
}
}