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

@@ -2,7 +2,7 @@ package wire
import (
"carrot_bbs/internal/handler"
"carrot_bbs/internal/pkg/sse"
"carrot_bbs/internal/pkg/ws"
"carrot_bbs/internal/service"
"github.com/google/wire"
@@ -35,6 +35,7 @@ var HandlerSet = wire.NewSet(
ProvideSystemMessageHandler,
ProvideGroupHandler,
ProvideScheduleHandler,
ProvideWSHandler,
)
// ProvideUserHandler 提供用户处理器
@@ -91,9 +92,9 @@ func ProvideMessageHandler(
messageService *service.MessageService,
userService service.UserService,
groupService service.GroupService,
sseHub *sse.Hub,
wsHub *ws.Hub,
) *handler.MessageHandler {
return handler.NewMessageHandler(chatService, messageService, userService, groupService, sseHub)
return handler.NewMessageHandler(chatService, messageService, userService, groupService, wsHub)
}
// ProvideSystemMessageHandler 提供系统消息处理器
@@ -118,3 +119,13 @@ func ProvideScheduleHandler(
) *handler.ScheduleHandler {
return handler.NewScheduleHandler(scheduleService, scheduleSyncService)
}
// ProvideWSHandler 提供WebSocket处理器
func ProvideWSHandler(
wsHub *ws.Hub,
chatService service.ChatService,
groupService service.GroupService,
jwtService *service.JWTService,
) *handler.WSHandler {
return handler.NewWSHandler(wsHub, chatService, groupService, jwtService)
}

View File

@@ -12,7 +12,7 @@ import (
"carrot_bbs/internal/pkg/openai"
"carrot_bbs/internal/pkg/redis"
"carrot_bbs/internal/pkg/s3"
"carrot_bbs/internal/pkg/sse"
"carrot_bbs/internal/pkg/ws"
"carrot_bbs/internal/repository"
"carrot_bbs/internal/service"
@@ -44,8 +44,8 @@ var InfrastructureSet = wire.NewSet(
ProvideBuiltinHooks,
ProvideModerationHooks,
// SSE Hub
ProvideSSEHub,
// WebSocket Hub
ProvideWSHub,
// 外部服务客户端
ProvideOpenAIClient,
@@ -156,9 +156,9 @@ func ProvideModerationHooks(
return moderationHooks
}
// ProvideSSEHub 提供 SSE Hub
func ProvideSSEHub() *sse.Hub {
return sse.NewHub()
// ProvideWSHub 提供 WebSocket Hub
func ProvideWSHub() *ws.Hub {
return ws.NewHub()
}
// ProvideOpenAIClient 提供 OpenAI 客户端

View File

@@ -11,7 +11,7 @@ import (
"carrot_bbs/internal/pkg/openai"
"carrot_bbs/internal/pkg/redis"
"carrot_bbs/internal/pkg/s3"
"carrot_bbs/internal/pkg/sse"
"carrot_bbs/internal/pkg/ws"
"carrot_bbs/internal/repository"
"carrot_bbs/internal/service"
@@ -89,9 +89,9 @@ func ProvidePushService(
pushRepo repository.PushRecordRepository,
deviceTokenRepo repository.DeviceTokenRepository,
messageRepo repository.MessageRepository,
sseHub *sse.Hub,
wsHub *ws.Hub,
) service.PushService {
return service.NewPushService(pushRepo, deviceTokenRepo, messageRepo, sseHub)
return service.NewPushService(pushRepo, deviceTokenRepo, messageRepo, wsHub)
}
// ProvideSystemMessageService 提供系统消息服务
@@ -183,11 +183,11 @@ func ProvideChannelService(channelRepo repository.ChannelRepository, cacheBacken
func ProvideChatService(
messageRepo repository.MessageRepository,
userRepo repository.UserRepository,
sseHub *sse.Hub,
wsHub *ws.Hub,
cacheBackend cache.Cache,
uploadService *service.UploadService,
) service.ChatService {
return service.NewChatService(messageRepo, userRepo, nil, sseHub, cacheBackend, uploadService)
return service.NewChatService(messageRepo, userRepo, nil, wsHub, cacheBackend, uploadService)
}
// ProvideScheduleService 提供日程服务
@@ -213,10 +213,10 @@ func ProvideGroupService(
messageRepo repository.MessageRepository,
requestRepo repository.GroupJoinRequestRepository,
notifyRepo repository.SystemNotificationRepository,
sseHub *sse.Hub,
wsHub *ws.Hub,
cacheBackend cache.Cache,
) service.GroupService {
return service.NewGroupService(groupRepo, userRepo, messageRepo, requestRepo, notifyRepo, sseHub, cacheBackend)
return service.NewGroupService(groupRepo, userRepo, messageRepo, requestRepo, notifyRepo, wsHub, cacheBackend)
}
// ProvideUploadService 提供上传服务
@@ -359,13 +359,13 @@ func ProvideHotRankWorker(cfg *config.Config, postRepo repository.PostRepository
func ProvideQRCodeLoginService(
redisClient *redis.Client,
sseHub *sse.Hub,
wsHub *ws.Hub,
jwtService *service.JWTService,
userService service.UserService,
activityService service.UserActivityService,
logService *service.LogService,
) *service.QRCodeLoginService {
return service.NewQRCodeLoginService(redisClient, sseHub, jwtService, userService, activityService, logService)
return service.NewQRCodeLoginService(redisClient, wsHub, jwtService, userService, activityService, logService)
}
// ProvideMaterialService 提供学习资料服务