diff --git a/internal/op/hub.go b/internal/op/hub.go index 1475085..5d98c97 100644 --- a/internal/op/hub.go +++ b/internal/op/hub.go @@ -247,6 +247,19 @@ func (h *Hub) IsOnline(userID string) bool { return ok } +func (h *Hub) OnlineCount(userID string) int { + c, ok := h.clients.Load(userID) + if !ok { + return 0 + } + c.lock.RLock() + defer c.lock.RUnlock() + if len(c.m) == 0 { + h.clients.CompareAndDelete(userID, c) + } + return len(c.m) +} + func (h *Hub) KickUser(userID string) error { if h.Closed() { return ErrAlreadyClosed diff --git a/internal/op/room.go b/internal/op/room.go index 305e468..b3a0e2b 100644 --- a/internal/op/room.go +++ b/internal/op/room.go @@ -413,11 +413,16 @@ func (r *Room) UnregisterClient(cli *Client) error { return r.hub.UnRegClient(cli) } -func (r *Room) IsOnline(userID string) bool { +func (r *Room) UserIsOnline(userID string) bool { r.lazyInitHub() return r.hub.IsOnline(userID) } +func (r *Room) UserOnlineCount(userID string) int { + r.lazyInitHub() + return r.hub.OnlineCount(userID) +} + func (r *Room) SetCurrentStatus(playing bool, seek float64, rate float64, timeDiff float64) *Status { return r.current.SetStatus(playing, seek, rate, timeDiff) } diff --git a/server/handlers/admin.go b/server/handlers/admin.go index ec370e9..88ae371 100644 --- a/server/handlers/admin.go +++ b/server/handlers/admin.go @@ -313,7 +313,7 @@ func genRoomMemberListResp(us []*dbModel.User, room *op.Room) []*model.RoomMembe UserID: v.ID, Username: v.Username, JoinAt: v.RoomMembers[0].CreatedAt.UnixMilli(), - IsOnline: room.IsOnline(v.ID), + OnlineCount: room.UserOnlineCount(v.ID), Role: v.RoomMembers[0].Role, Status: v.RoomMembers[0].Status, RoomID: v.RoomMembers[0].RoomID, diff --git a/server/model/member.go b/server/model/member.go index 988fc6b..52e6623 100644 --- a/server/model/member.go +++ b/server/model/member.go @@ -10,7 +10,7 @@ type RoomMembersResp struct { UserID string `json:"userId"` Username string `json:"username"` JoinAt int64 `json:"joinAt"` - IsOnline bool `json:"isOnline"` + OnlineCount int `json:"onlineCount"` Role dbModel.RoomMemberRole `json:"role"` Status dbModel.RoomMemberStatus `json:"status"` RoomID string `json:"roomId"`