feat(websocket): integrate WebSocket support and refactor SSE handling
- 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:
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
"carrot_bbs/internal/model"
|
||||
"carrot_bbs/internal/pkg/redis"
|
||||
"carrot_bbs/internal/pkg/sse"
|
||||
"carrot_bbs/internal/pkg/ws"
|
||||
)
|
||||
|
||||
// Lua 脚本:原子性地检查状态并更新为 scanned
|
||||
@@ -54,7 +54,7 @@ type QRCodeSession struct {
|
||||
// QRCodeLoginService 二维码登录服务
|
||||
type QRCodeLoginService struct {
|
||||
redis *redis.Client
|
||||
sseHub *sse.Hub
|
||||
wsHub *ws.Hub
|
||||
jwtService *JWTService
|
||||
userService UserService
|
||||
activityService UserActivityService
|
||||
@@ -62,10 +62,10 @@ type QRCodeLoginService struct {
|
||||
}
|
||||
|
||||
// NewQRCodeLoginService 创建二维码登录服务
|
||||
func NewQRCodeLoginService(redis *redis.Client, sseHub *sse.Hub, jwtService *JWTService, userService UserService, activityService UserActivityService, logService *LogService) *QRCodeLoginService {
|
||||
func NewQRCodeLoginService(redis *redis.Client, wsHub *ws.Hub, jwtService *JWTService, userService UserService, activityService UserActivityService, logService *LogService) *QRCodeLoginService {
|
||||
return &QRCodeLoginService{
|
||||
redis: redis,
|
||||
sseHub: sseHub,
|
||||
wsHub: wsHub,
|
||||
jwtService: jwtService,
|
||||
userService: userService,
|
||||
activityService: activityService,
|
||||
@@ -170,7 +170,7 @@ func (s *QRCodeLoginService) Scan(ctx context.Context, sessionID, userID string)
|
||||
"avatar": user.Avatar,
|
||||
},
|
||||
}
|
||||
s.sseHub.PublishToUser(sessionID, "scanned", payload)
|
||||
s.wsHub.PublishToUser(sessionID, "scanned", payload)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -242,7 +242,7 @@ func (s *QRCodeLoginService) Confirm(ctx context.Context, sessionID, userID stri
|
||||
RefreshToken: refreshToken,
|
||||
User: user,
|
||||
}
|
||||
s.sseHub.PublishToUser(sessionID, "confirmed", response)
|
||||
s.wsHub.PublishToUser(sessionID, "confirmed", response)
|
||||
|
||||
return response, nil
|
||||
}
|
||||
@@ -275,14 +275,14 @@ func (s *QRCodeLoginService) Cancel(ctx context.Context, sessionID, userID strin
|
||||
}
|
||||
|
||||
// 推送取消事件
|
||||
s.sseHub.PublishToUser(sessionID, "cancelled", map[string]interface{}{})
|
||||
s.wsHub.PublishToUser(sessionID, "cancelled", map[string]interface{}{})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSSEHub 获取SSE Hub
|
||||
func (s *QRCodeLoginService) GetSSEHub() *sse.Hub {
|
||||
return s.sseHub
|
||||
// GetWSHub 获取WS Hub
|
||||
func (s *QRCodeLoginService) GetWSHub() *ws.Hub {
|
||||
return s.wsHub
|
||||
}
|
||||
|
||||
// GetUserService 获取用户服务
|
||||
|
||||
Reference in New Issue
Block a user