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

main
NorthLan 2 years ago
parent dec70a2f77
commit af91060278

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

Loading…
Cancel
Save