feat(ws): 在线状态管理(Redis SET + TTL 心跳续期)
- app/ws/online_service.go: 上线/下线/心跳续期/批量查询在线状态
- 集成到 WebSocket handler: 连接时写入 Redis, 断线时清除
- client.go: 添加 DisconnectHandler 回调
- Redis 键: echo:user:online (SET) + echo:user:status:{uid} (TTL 60s)
Made-with: Cursor
This commit is contained in:
@@ -28,6 +28,7 @@ type App struct {
|
||||
WSHandler *wsApp.Handler // WebSocket 连接处理器
|
||||
Hub *ws.Hub // WebSocket Hub 连接管理
|
||||
PubSub *ws.PubSub // Redis Pub/Sub 消息路由
|
||||
OnlineService *wsApp.OnlineService // 在线状态管理服务
|
||||
ContactController *contactController.ContactController // 联系人控制器
|
||||
}
|
||||
|
||||
@@ -43,6 +44,7 @@ func NewApp(
|
||||
wsHandler *wsApp.Handler,
|
||||
hub *ws.Hub,
|
||||
pubsub *ws.PubSub,
|
||||
onlineService *wsApp.OnlineService,
|
||||
contactCtrl *contactController.ContactController,
|
||||
) *App {
|
||||
return &App{
|
||||
@@ -56,6 +58,7 @@ func NewApp(
|
||||
WSHandler: wsHandler,
|
||||
Hub: hub,
|
||||
PubSub: pubsub,
|
||||
OnlineService: onlineService,
|
||||
ContactController: contactCtrl,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,11 +47,12 @@ func InitializeApp(cfg *config.Config) (*App, error) {
|
||||
userManageController := controller2.NewUserManageController(userManageService)
|
||||
hub := ws.ProvideHub()
|
||||
pubSub := ws.ProvidePubSub(client, hub)
|
||||
handler := ws.ProvideWSHandler(hub, pubSub, jwtConfig)
|
||||
onlineService := ws.ProvideOnlineService(client, hub, pubSub)
|
||||
handler := ws.ProvideWSHandler(hub, pubSub, jwtConfig, onlineService)
|
||||
friendshipDAO := dao3.NewFriendshipDAO(gormDB)
|
||||
friendGroupDAO := dao3.NewFriendGroupDAO(gormDB)
|
||||
contactService := service3.NewContactService(friendshipDAO, friendGroupDAO, pubSub)
|
||||
contactController := controller3.NewContactController(contactService)
|
||||
app := NewApp(cfg, gormDB, client, authService, authController, adminAuthController, userManageController, handler, hub, pubSub, contactController)
|
||||
app := NewApp(cfg, gormDB, client, authService, authController, adminAuthController, userManageController, handler, hub, pubSub, onlineService, contactController)
|
||||
return app, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user