diff --git a/server/internal/session/manager.go b/server/internal/session/manager.go index a06b147f..2e77bf42 100644 --- a/server/internal/session/manager.go +++ b/server/internal/session/manager.go @@ -184,6 +184,30 @@ func (manager *SessionManager) Broadcast(v interface{}, exclude interface{}) err return err } } + + return nil +} + +func (manager *SessionManager) AdminBroadcast(v interface{}, exclude interface{}) error { + manager.mu.Lock() + defer manager.mu.Unlock() + + for id, session := range manager.members { + if !session.connected || !session.admin { + continue + } + + if exclude != nil { + if in, _ := utils.ArrayIn(id, exclude); in { + continue + } + } + + if err := session.Send(v); err != nil { + return err + } + } + return nil } diff --git a/server/internal/types/session.go b/server/internal/types/session.go index da4eb414..3e765e63 100644 --- a/server/internal/types/session.go +++ b/server/internal/types/session.go @@ -43,6 +43,7 @@ type SessionManager interface { Destroy(id string) Clear() error Broadcast(v interface{}, exclude interface{}) error + AdminBroadcast(v interface{}, exclude interface{}) error OnHost(listener func(id string)) OnHostCleared(listener func(id string)) OnDestroy(listener func(id string, session Session)) diff --git a/server/internal/websocket/broadcast.go b/server/internal/websocket/broadcast.go index 1eb8bcd5..865c0319 100644 --- a/server/internal/websocket/broadcast.go +++ b/server/internal/websocket/broadcast.go @@ -25,7 +25,7 @@ func (h *MessageHandler) boradcastCreate(session types.Session, payload *message } } - if err := h.boradcastStatus(session); err != nil { + if err := h.boradcastStatus(nil); err != nil { return err } @@ -40,7 +40,7 @@ func (h *MessageHandler) boradcastDestroy(session types.Session) error { h.broadcast.Destroy() - if err := h.boradcastStatus(session); err != nil { + if err := h.boradcastStatus(nil); err != nil { return err } @@ -48,6 +48,21 @@ func (h *MessageHandler) boradcastDestroy(session types.Session) error { } func (h *MessageHandler) boradcastStatus(session types.Session) error { + // if no session, broadcast change + if session == nil { + if err := h.sessions.AdminBroadcast( + message.BroadcastStatus{ + Event: event.BORADCAST_STATUS, + IsActive: h.broadcast.IsActive(), + URL: h.broadcast.GetUrl(), + }, nil); err != nil { + h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.BORADCAST_STATUS) + return err + } + + return nil + } + if !session.Admin() { h.logger.Debug().Msg("user not admin") return nil