diff --git a/server/internal/session/manager.go b/server/internal/session/manager.go index 15b2d4f..8e51bc5 100644 --- a/server/internal/session/manager.go +++ b/server/internal/session/manager.go @@ -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)) }) } diff --git a/server/internal/types/session.go b/server/internal/types/session.go index 1c8acab..acdcac6 100644 --- a/server/internal/types/session.go +++ b/server/internal/types/session.go @@ -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)) } diff --git a/server/internal/websocket/handler.go b/server/internal/websocket/handler.go index e350752..fddd63f 100644 --- a/server/internal/websocket/handler.go +++ b/server/internal/websocket/handler.go @@ -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) }