Archived
2
0

add admin broadcast.

This commit is contained in:
Miroslav Šedivý 2021-12-11 13:52:31 +01:00
parent 8db06a7625
commit 42fdc43ff5
3 changed files with 42 additions and 2 deletions

View File

@ -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
}

View File

@ -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))

View File

@ -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