Feat: member online count

pull/134/head
zijiren233 1 year ago
parent a22d14386c
commit b966db7da1

@ -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

@ -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)
}

@ -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,

@ -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"`

Loading…
Cancel
Save