Broadcast should not return error.

This commit is contained in:
Miroslav Šedivý 2020-11-18 20:30:33 +01:00
parent 3f4914d260
commit da7e6f7728
11 changed files with 103 additions and 90 deletions

View File

@ -24,13 +24,11 @@ func (h *RoomHandler) ControlRequest(w http.ResponseWriter, r *http.Request) {
h.sessions.SetHost(session) h.sessions.SetHost(session)
if err := h.sessions.Broadcast( h.sessions.Broadcast(
message.Control{ message.Control{
Event: event.CONTROL_LOCKED, Event: event.CONTROL_LOCKED,
ID: session.ID(), ID: session.ID(),
}, nil); err != nil { }, nil)
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_LOCKED)
}
utils.HttpSuccess(w) utils.HttpSuccess(w)
} }
@ -44,13 +42,11 @@ func (h *RoomHandler) ControlRelease(w http.ResponseWriter, r *http.Request) {
h.sessions.ClearHost() h.sessions.ClearHost()
if err := h.sessions.Broadcast( h.sessions.Broadcast(
message.Control{ message.Control{
Event: event.CONTROL_RELEASE, Event: event.CONTROL_RELEASE,
ID: session.ID(), ID: session.ID(),
}, nil); err != nil { }, nil)
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_RELEASE)
}
utils.HttpSuccess(w) utils.HttpSuccess(w)
} }

View File

@ -42,16 +42,15 @@ func (h *RoomHandler) ScreenConfigurationChange(w http.ResponseWriter, r *http.R
} }
session := auth.GetSession(r) session := auth.GetSession(r)
if err := h.sessions.Broadcast(
h.sessions.Broadcast(
message.ScreenResolution{ message.ScreenResolution{
Event: event.SCREEN_RESOLUTION, Event: event.SCREEN_RESOLUTION,
ID: session.ID(), ID: session.ID(),
Width: data.Width, Width: data.Width,
Height: data.Height, Height: data.Height,
Rate: data.Rate, Rate: data.Rate,
}, nil); err != nil { }, nil)
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.SCREEN_RESOLUTION)
}
utils.HttpSuccess(w, data) utils.HttpSuccess(w, data)
} }

View File

@ -142,7 +142,7 @@ func (manager *SessionManagerCtx) Members() []types.Session {
return sessions return sessions
} }
func (manager *SessionManagerCtx) Broadcast(v interface{}, exclude interface{}) error { func (manager *SessionManagerCtx) Broadcast(v interface{}, exclude interface{}) {
manager.membersMu.Lock() manager.membersMu.Lock()
defer manager.membersMu.Unlock() defer manager.membersMu.Unlock()
@ -158,11 +158,9 @@ func (manager *SessionManagerCtx) Broadcast(v interface{}, exclude interface{})
} }
if err := session.Send(v); err != nil { if err := session.Send(v); err != nil {
return err manager.logger.Warn().Err(err).Msgf("broadcasting event has failed")
} }
} }
return nil
} }
// --- // ---

View File

@ -67,20 +67,13 @@ func (session *SessionCtx) SetConnected(connected bool) {
} }
func (session *SessionCtx) Disconnect(reason string) error { func (session *SessionCtx) Disconnect(reason string) error {
if session.socket == nil { session.SetConnected(false)
return nil
}
// TODO: Refcator return session.Send(
if err := session.Send(&message.Disconnect{ message.Disconnect{
Event: event.SYSTEM_DISCONNECT, Event: event.SYSTEM_DISCONNECT,
Message: reason, Message: reason,
}); err != nil { })
return err
}
session.SetConnected(false)
return nil
} }
func (session *SessionCtx) Send(v interface{}) error { func (session *SessionCtx) Send(v interface{}) error {

View File

@ -29,7 +29,7 @@ type SessionManager interface {
Admins() []Session Admins() []Session
Members() []Session Members() []Session
Broadcast(v interface{}, exclude interface{}) error Broadcast(v interface{}, exclude interface{})
OnHost(listener func(session Session)) OnHost(listener func(session Session))
OnHostCleared(listener func(session Session)) OnHostCleared(listener func(session Session))

View File

@ -19,11 +19,13 @@ func (h *MessageHandlerCtx) adminLock(session types.Session) error {
h.locked = true h.locked = true
return h.sessions.Broadcast( h.sessions.Broadcast(
message.Admin{ message.Admin{
Event: event.ADMIN_LOCK, Event: event.ADMIN_LOCK,
ID: session.ID(), ID: session.ID(),
}, nil) }, nil)
return nil
} }
func (h *MessageHandlerCtx) adminUnlock(session types.Session) error { func (h *MessageHandlerCtx) adminUnlock(session types.Session) error {
@ -39,11 +41,13 @@ func (h *MessageHandlerCtx) adminUnlock(session types.Session) error {
h.locked = false h.locked = false
return h.sessions.Broadcast( h.sessions.Broadcast(
message.Admin{ message.Admin{
Event: event.ADMIN_UNLOCK, Event: event.ADMIN_UNLOCK,
ID: session.ID(), ID: session.ID(),
}, nil) }, nil)
return nil
} }
func (h *MessageHandlerCtx) adminControl(session types.Session) error { func (h *MessageHandlerCtx) adminControl(session types.Session) error {
@ -56,21 +60,23 @@ func (h *MessageHandlerCtx) adminControl(session types.Session) error {
h.sessions.SetHost(session) h.sessions.SetHost(session)
if host != nil { if host != nil {
return h.sessions.Broadcast( h.sessions.Broadcast(
message.AdminTarget{ message.AdminTarget{
Event: event.ADMIN_CONTROL, Event: event.ADMIN_CONTROL,
ID: session.ID(), ID: session.ID(),
Target: host.ID(), Target: host.ID(),
}, nil) }, nil)
} } else {
h.sessions.Broadcast(
return h.sessions.Broadcast(
message.Admin{ message.Admin{
Event: event.ADMIN_CONTROL, Event: event.ADMIN_CONTROL,
ID: session.ID(), ID: session.ID(),
}, nil) }, nil)
} }
return nil
}
func (h *MessageHandlerCtx) adminRelease(session types.Session) error { func (h *MessageHandlerCtx) adminRelease(session types.Session) error {
if !session.Admin() { if !session.Admin() {
h.logger.Debug().Msg("user not admin") h.logger.Debug().Msg("user not admin")
@ -81,21 +87,23 @@ func (h *MessageHandlerCtx) adminRelease(session types.Session) error {
h.sessions.ClearHost() h.sessions.ClearHost()
if host != nil { if host != nil {
return h.sessions.Broadcast( h.sessions.Broadcast(
message.AdminTarget{ message.AdminTarget{
Event: event.ADMIN_RELEASE, Event: event.ADMIN_RELEASE,
ID: session.ID(), ID: session.ID(),
Target: host.ID(), Target: host.ID(),
}, nil) }, nil)
} } else {
h.sessions.Broadcast(
return h.sessions.Broadcast(
message.Admin{ message.Admin{
Event: event.ADMIN_RELEASE, Event: event.ADMIN_RELEASE,
ID: session.ID(), ID: session.ID(),
}, nil) }, nil)
} }
return nil
}
func (h *MessageHandlerCtx) adminGive(session types.Session, payload *message.Admin) error { func (h *MessageHandlerCtx) adminGive(session types.Session, payload *message.Admin) error {
if !session.Admin() { if !session.Admin() {
h.logger.Debug().Msg("user not admin") h.logger.Debug().Msg("user not admin")
@ -110,12 +118,14 @@ func (h *MessageHandlerCtx) adminGive(session types.Session, payload *message.Ad
h.sessions.SetHost(target) h.sessions.SetHost(target)
return h.sessions.Broadcast( h.sessions.Broadcast(
message.AdminTarget{ message.AdminTarget{
Event: event.CONTROL_GIVE, Event: event.CONTROL_GIVE,
ID: session.ID(), ID: session.ID(),
Target: target.ID(), Target: target.ID(),
}, nil) }, nil)
return nil
} }
func (h *MessageHandlerCtx) adminKick(session types.Session, payload *message.Admin) error { func (h *MessageHandlerCtx) adminKick(session types.Session, payload *message.Admin) error {
@ -139,10 +149,12 @@ func (h *MessageHandlerCtx) adminKick(session types.Session, payload *message.Ad
return err return err
} }
return h.sessions.Broadcast( h.sessions.Broadcast(
message.AdminTarget{ message.AdminTarget{
Event: event.ADMIN_KICK, Event: event.ADMIN_KICK,
Target: target.ID(), Target: target.ID(),
ID: session.ID(), ID: session.ID(),
}, []string{payload.ID}) }, []string{payload.ID})
return nil
} }

View File

@ -15,11 +15,13 @@ func (h *MessageHandlerCtx) controlRelease(session types.Session) error {
h.logger.Debug().Str("id", session.ID()).Msgf("host called %s", event.CONTROL_RELEASE) h.logger.Debug().Str("id", session.ID()).Msgf("host called %s", event.CONTROL_RELEASE)
h.sessions.ClearHost() h.sessions.ClearHost()
return h.sessions.Broadcast( h.sessions.Broadcast(
message.Control{ message.Control{
Event: event.CONTROL_RELEASE, Event: event.CONTROL_RELEASE,
ID: session.ID(), ID: session.ID(),
}, nil) }, nil)
return nil
} }
func (h *MessageHandlerCtx) controlRequest(session types.Session) error { func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
@ -30,15 +32,15 @@ func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
h.sessions.SetHost(session) h.sessions.SetHost(session)
// let everyone know // let everyone know
return h.sessions.Broadcast( h.sessions.Broadcast(
message.Control{ message.Control{
Event: event.CONTROL_LOCKED, Event: event.CONTROL_LOCKED,
ID: session.ID(), ID: session.ID(),
}, nil) }, nil)
} } else {
// tell session there is a host // tell session there is a host
if err := session.Send(message.Control{ if err := session.Send(
message.Control{
Event: event.CONTROL_REQUEST, Event: event.CONTROL_REQUEST,
ID: host.ID(), ID: host.ID(),
}); err != nil { }); err != nil {
@ -46,12 +48,16 @@ func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
} }
// tell host session wants to be host // tell host session wants to be host
return host.Send(message.Control{ return host.Send(
message.Control{
Event: event.CONTROL_REQUESTING, Event: event.CONTROL_REQUESTING,
ID: session.ID(), ID: session.ID(),
}) })
} }
return nil
}
func (h *MessageHandlerCtx) controlGive(session types.Session, payload *message.Control) error { func (h *MessageHandlerCtx) controlGive(session types.Session, payload *message.Control) error {
if !session.IsHost() { if !session.IsHost() {
h.logger.Debug().Str("id", session.ID()).Msg("is not the host") h.logger.Debug().Str("id", session.ID()).Msg("is not the host")
@ -66,12 +72,14 @@ func (h *MessageHandlerCtx) controlGive(session types.Session, payload *message.
h.sessions.SetHost(target) h.sessions.SetHost(target)
return h.sessions.Broadcast( h.sessions.Broadcast(
message.ControlTarget{ message.ControlTarget{
Event: event.CONTROL_GIVE, Event: event.CONTROL_GIVE,
ID: session.ID(), ID: session.ID(),
Target: target.ID(), Target: target.ID(),
}, nil) }, nil)
return nil
} }
func (h *MessageHandlerCtx) controlClipboard(session types.Session, payload *message.Clipboard) error { func (h *MessageHandlerCtx) controlClipboard(session types.Session, payload *message.Clipboard) error {

View File

@ -17,7 +17,7 @@ func (h *MessageHandlerCtx) screenSizeChange(session types.Session, payload *mes
return nil return nil
} }
return h.sessions.Broadcast( h.sessions.Broadcast(
message.ScreenResolution{ message.ScreenResolution{
Event: event.SCREEN_RESOLUTION, Event: event.SCREEN_RESOLUTION,
ID: session.ID(), ID: session.ID(),
@ -25,6 +25,8 @@ func (h *MessageHandlerCtx) screenSizeChange(session types.Session, payload *mes
Height: payload.Height, Height: payload.Height,
Rate: payload.Rate, Rate: payload.Rate,
}, nil) }, nil)
return nil
} }
func (h *MessageHandlerCtx) screenSize(session types.Session) error { func (h *MessageHandlerCtx) screenSize(session types.Session) error {

View File

@ -39,7 +39,8 @@ func (h *MessageHandlerCtx) SessionConnected(session types.Session) error {
} }
// send list of members to session // send list of members to session
if err := session.Send(message.MembersList{ if err := session.Send(
message.MembersList{
Event: event.MEMBER_LIST, Event: event.MEMBER_LIST,
Memebers: members, Memebers: members,
}); err != nil { }); err != nil {
@ -54,7 +55,8 @@ func (h *MessageHandlerCtx) SessionConnected(session types.Session) error {
// tell session there is a host // tell session there is a host
host := h.sessions.GetHost() host := h.sessions.GetHost()
if host != nil { if host != nil {
if err := session.Send(message.Control{ if err := session.Send(
message.Control{
Event: event.CONTROL_LOCKED, Event: event.CONTROL_LOCKED,
ID: host.ID(), ID: host.ID(),
}); err != nil { }); err != nil {
@ -63,13 +65,15 @@ func (h *MessageHandlerCtx) SessionConnected(session types.Session) error {
} }
// let everyone know there is a new session // let everyone know there is a new session
return h.sessions.Broadcast( h.sessions.Broadcast(
message.Member{ message.Member{
Event: event.MEMBER_CONNECTED, Event: event.MEMBER_CONNECTED,
ID: session.ID(), ID: session.ID(),
Name: session.Name(), Name: session.Name(),
Admin: session.Admin(), Admin: session.Admin(),
}, nil) }, nil)
return nil
} }
func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error { func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
@ -77,20 +81,19 @@ func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
if session.IsHost() { if session.IsHost() {
h.sessions.ClearHost() h.sessions.ClearHost()
// gracefully handle error h.sessions.Broadcast(
if err := h.sessions.Broadcast(
message.Control{ message.Control{
Event: event.CONTROL_RELEASE, Event: event.CONTROL_RELEASE,
ID: session.ID(), ID: session.ID(),
}, nil); err != nil { }, nil)
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_RELEASE)
}
} }
// let everyone know session disconnected // let everyone know session disconnected
return h.sessions.Broadcast( h.sessions.Broadcast(
message.MemberDisconnected{ message.MemberDisconnected{
Event: event.MEMBER_DISCONNECTED, Event: event.MEMBER_DISCONNECTED,
ID: session.ID(), ID: session.ID(),
}, nil); }, nil);
return nil
} }

View File

@ -12,7 +12,8 @@ func (h *MessageHandlerCtx) signalProvide(session types.Session) error {
return err return err
} }
return session.Send(message.SignalProvide{ return session.Send(
message.SignalProvide{
Event: event.SIGNAL_PROVIDE, Event: event.SIGNAL_PROVIDE,
ID: session.ID(), ID: session.ID(),
SDP: sdp, SDP: sdp,

View File

@ -97,7 +97,8 @@ func (ws *WebSocketManagerCtx) Start() {
break break
} }
if err := session.Send(message.Clipboard{ if err := session.Send(
message.Clipboard{
Event: event.CONTROL_CLIPBOARD, Event: event.CONTROL_CLIPBOARD,
Text: text, Text: text,
}); err != nil { }); err != nil {