Archived
2
0

unlock room if no admins online

This commit is contained in:
Craig 2020-04-06 20:14:08 +00:00
parent 82ca6e13ca
commit 414b5a8015
3 changed files with 24 additions and 4 deletions

View File

@ -88,6 +88,21 @@ func (manager *SessionManager) Get(id string) (types.Session, bool) {
return session, ok
}
func (manager *SessionManager) Admins() []*types.Member {
members := []*types.Member{}
for _, session := range manager.members {
if !session.connected || !session.admin {
continue
}
member := session.Member()
if member != nil {
members = append(members, member)
}
}
return members
}
func (manager *SessionManager) Members() []*types.Member {
members := []*types.Member{}
for _, session := range manager.members {
@ -113,7 +128,7 @@ func (manager *SessionManager) Destroy(id string) error {
manager.remote.StopStream()
}
manager.emmiter.Emit("destroyed", id)
manager.emmiter.Emit("destroyed", id, session)
return err
}
@ -155,9 +170,9 @@ func (manager *SessionManager) OnHostCleared(listener func(id string)) {
})
}
func (manager *SessionManager) OnDestroy(listener func(id string)) {
func (manager *SessionManager) OnDestroy(listener func(id string, session types.Session)) {
manager.emmiter.On("destroyed", func(payload ...interface{}) {
listener(payload[0].(string))
listener(payload[0].(string), payload[1].(*Session))
})
}

View File

@ -36,12 +36,13 @@ type SessionManager interface {
Has(id string) bool
Get(id string) (Session, bool)
Members() []*Member
Admins() []*Member
Destroy(id string) error
Clear() error
Broadcast(v interface{}, exclude interface{}) error
OnHost(listener func(id string))
OnHostCleared(listener func(id string))
OnDestroy(listener func(id string))
OnDestroy(listener func(id string, session Session))
OnCreated(listener func(id string, session Session))
OnConnected(listener func(id string, session Session))
}

View File

@ -42,6 +42,10 @@ func (h *MessageHandler) Connected(id string, socket *WebSocket) (bool, string,
}
func (h *MessageHandler) Disconnected(id string) error {
if h.locked && len(h.sessions.Admins()) == 0 {
h.locked = false
}
return h.sessions.Destroy(id)
}