feat: 提供Member的遍历方法,提供从sessionId获取session的方法。

main
NorthLan 3 years ago
parent dec70a2f77
commit af91060278

@ -51,24 +51,48 @@ func (c *Group) Member(uid int64) (*session.Session, error) {
return nil, ErrMemberNotFound return nil, ErrMemberNotFound
} }
// Members returns all members in current group // MemberBySID returns specified sId's session
func (c *Group) Members() map[int64]*session.Session { func (c *Group) MemberBySID(id int64) (*session.Session, error) {
c.mu.RLock() c.mu.RLock()
defer c.mu.RUnlock() defer c.mu.RUnlock()
return c.sessions for sId, s := range c.sessions {
if sId == id {
return s, nil
}
}
return nil, ErrMemberNotFound
}
func (c *Group) Members() []*session.Session {
var resp []*session.Session
c.PeekMembers(func(_ int64, s *session.Session) bool {
resp = append(resp, s)
return true
})
return resp
} }
// MemberIDs returns all member's UID in current group // PeekMembers returns all members in current group
func (c *Group) MemberIDs() []int64 { func (c *Group) PeekMembers(fn func(sId int64, s *session.Session) bool) {
c.mu.RLock() c.mu.RLock()
defer c.mu.RUnlock() defer c.mu.RUnlock()
var members []int64 for sId, s := range c.sessions {
for _, s := range c.sessions { if !fn(sId, s) {
members = append(members, s.UID()) break
}
} }
}
// MemberUIDs returns all member's UID in current group
func (c *Group) MemberUIDs() []int64 {
var members []int64
c.PeekMembers(func(_ int64, s *session.Session) bool {
members = append(members, s.UID())
return false
})
return members return members
} }

Loading…
Cancel
Save