extract member state to custom struct.

This commit is contained in:
Miroslav Šedivý 2020-12-03 16:10:52 +01:00
parent 5f3b9f72cf
commit d7b5bd6842
9 changed files with 64 additions and 77 deletions

View File

@ -240,20 +240,14 @@ func (manager *SessionManagerCtx) OnDisconnected(listener func(session types.Ses
}) })
} }
func (manager *SessionManagerCtx) OnReceivingStarted(listener func(session types.Session)) { func (manager *SessionManagerCtx) OnProfileChanged(listener func(session types.Session)) {
manager.emmiter.On("receiving_started", func(payload ...interface{}) { manager.emmiter.On("profile_changed", func(payload ...interface{}) {
listener(payload[0].(*SessionCtx)) listener(payload[0].(*SessionCtx))
}) })
} }
func (manager *SessionManagerCtx) OnReceivingStopped(listener func(session types.Session)) { func (manager *SessionManagerCtx) OnStateChanged(listener func(session types.Session)) {
manager.emmiter.On("receiving_stopped", func(payload ...interface{}) { manager.emmiter.On("state_changed", func(payload ...interface{}) {
listener(payload[0].(*SessionCtx))
})
}
func (manager *SessionManagerCtx) OnProfileUpdated(listener func(session types.Session)) {
manager.emmiter.On("profile_updated", func(payload ...interface{}) {
listener(payload[0].(*SessionCtx)) listener(payload[0].(*SessionCtx))
}) })
} }

View File

@ -61,7 +61,7 @@ func (session *SessionCtx) CanAccessClipboard() bool {
func (session *SessionCtx) SetProfile(profile types.MemberProfile) { func (session *SessionCtx) SetProfile(profile types.MemberProfile) {
session.profile = profile session.profile = profile
session.manager.emmiter.Emit("profile_updated", session) session.manager.emmiter.Emit("profile_changed", session)
} }
// --- // ---
@ -140,12 +140,10 @@ func (session *SessionCtx) SetWebRTCPeer(webrtc_peer types.WebRTCPeer) {
} }
func (session *SessionCtx) SetWebRTCConnected(connected bool) { func (session *SessionCtx) SetWebRTCConnected(connected bool) {
if connected { session.webrtc_connected = connected
session.webrtc_connected = true session.manager.emmiter.Emit("state_changed", session)
session.manager.emmiter.Emit("receiving_started", session)
} else { if !connected {
session.webrtc_connected = false
session.manager.emmiter.Emit("receiving_stopped", session)
session.webrtc_peer = nil session.webrtc_peer = nil
} }
} }

View File

@ -17,9 +17,8 @@ const (
MEMBER_DELETED = "member/deleted" MEMBER_DELETED = "member/deleted"
MEMBER_CONNECTED = "member/connected" MEMBER_CONNECTED = "member/connected"
MEMBER_DISCONNECTED = "member/disconnected" MEMBER_DISCONNECTED = "member/disconnected"
MEMBER_RECEIVING_STARTED = "member/receiving/started" MEMBER_PROFILE = "member/profile"
MEMBER_RECEIVING_STOPPED = "member/receiving/stopped" MEMBER_STATE = "member/state"
MEMBER_PROFILE_UPDATED = "member/profile/updated"
) )
const ( const (

View File

@ -66,12 +66,18 @@ type MemberProfile struct {
CanAccessClipboard bool `json:"can_access_clipboard"` CanAccessClipboard bool `json:"can_access_clipboard"`
} }
type MemberState struct {
Event string `json:"event,omitempty"`
ID string `json:"id,omitempty"`
IsConnected bool `json:"is_connected"`
IsReceiving bool `json:"is_receiving"`
}
type MemberData struct { type MemberData struct {
Event string `json:"event,omitempty"` Event string `json:"event,omitempty"`
ID string `json:"id"` ID string `json:"id"`
Profile MemberProfile `json:"profile"` Profile MemberProfile `json:"profile"`
IsConnected bool `json:"is_connected"` State MemberState `json:"state"`
IsReceiving bool `json:"is_receiving"`
} }
///////////////////////////// /////////////////////////////

View File

@ -62,9 +62,8 @@ type SessionManager interface {
OnDeleted(listener func(session Session)) OnDeleted(listener func(session Session))
OnConnected(listener func(session Session)) OnConnected(listener func(session Session))
OnDisconnected(listener func(session Session)) OnDisconnected(listener func(session Session))
OnReceivingStarted(listener func(session Session)) OnProfileChanged(listener func(session Session))
OnReceivingStopped(listener func(session Session)) OnStateChanged(listener func(session Session))
OnProfileUpdated(listener func(session Session))
ImplicitHosting() bool ImplicitHosting() bool

View File

@ -154,8 +154,10 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (string, bool
connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
switch state { switch state {
case webrtc.PeerConnectionStateDisconnected: case webrtc.PeerConnectionStateDisconnected:
manager.logger.Info().Str("id", session.ID()).Msg("peer disconnected successfully")
session.SetWebRTCConnected(false)
case webrtc.PeerConnectionStateFailed: case webrtc.PeerConnectionStateFailed:
manager.logger.Info().Str("id", session.ID()).Msg("peer disconnected") manager.logger.Warn().Str("id", session.ID()).Msg("peer disconnected with error")
session.SetWebRTCConnected(false) session.SetWebRTCConnected(false)
case webrtc.PeerConnectionStateConnected: case webrtc.PeerConnectionStateConnected:
manager.logger.Info().Str("id", session.ID()).Msg("peer connected") manager.logger.Info().Str("id", session.ID()).Msg("peer connected")

View File

@ -21,8 +21,10 @@ func (h *MessageHandlerCtx) SessionCreated(session types.Session) error {
CanHost: session.CanHost(), CanHost: session.CanHost(),
CanAccessClipboard: session.CanAccessClipboard(), CanAccessClipboard: session.CanAccessClipboard(),
}, },
State: message.MemberState{
IsConnected: session.IsConnected(), IsConnected: session.IsConnected(),
IsReceiving: session.IsReceiving(), IsReceiving: session.IsReceiving(),
},
}, nil) }, nil)
return nil return nil
@ -90,31 +92,11 @@ func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
return nil return nil
} }
func (h *MessageHandlerCtx) SessionReceivingStarted(session types.Session) error { func (h *MessageHandlerCtx) SessionProfileChanged(session types.Session) error {
h.sessions.Broadcast(
message.MemberID{
Event: event.MEMBER_RECEIVING_STARTED,
ID: session.ID(),
}, nil);
return nil
}
func (h *MessageHandlerCtx) SessionReceivingStopped(session types.Session) error {
h.sessions.Broadcast(
message.MemberID{
Event: event.MEMBER_RECEIVING_STOPPED,
ID: session.ID(),
}, nil);
return nil
}
func (h *MessageHandlerCtx) SessionProfileUpdated(session types.Session) error {
// TODO: Join structs? // TODO: Join structs?
h.sessions.Broadcast( h.sessions.Broadcast(
message.MemberProfile{ message.MemberProfile{
Event: event.MEMBER_PROFILE_UPDATED, Event: event.MEMBER_PROFILE,
ID: session.ID(), ID: session.ID(),
Name: session.Name(), Name: session.Name(),
IsAdmin: session.IsAdmin(), IsAdmin: session.IsAdmin(),
@ -127,3 +109,16 @@ func (h *MessageHandlerCtx) SessionProfileUpdated(session types.Session) error {
return nil return nil
} }
func (h *MessageHandlerCtx) SessionStateChanged(session types.Session) error {
// TODO: Join structs?
h.sessions.Broadcast(
message.MemberState{
Event: event.MEMBER_STATE,
ID: session.ID(),
IsConnected: session.IsConnected(),
IsReceiving: session.IsReceiving(),
}, nil)
return nil
}

View File

@ -37,8 +37,10 @@ func (h *MessageHandlerCtx) systemInit(session types.Session) error {
CanHost: session.CanHost(), CanHost: session.CanHost(),
CanAccessClipboard: session.CanAccessClipboard(), CanAccessClipboard: session.CanAccessClipboard(),
}, },
State: message.MemberState{
IsConnected: session.IsConnected(), IsConnected: session.IsConnected(),
IsReceiving: session.IsReceiving(), IsReceiving: session.IsReceiving(),
},
} }
} }

View File

@ -81,27 +81,19 @@ func (ws *WebSocketManagerCtx) Start() {
} }
}) })
ws.sessions.OnReceivingStarted(func(session types.Session) { ws.sessions.OnProfileChanged(func(session types.Session) {
if err := ws.handler.SessionReceivingStarted(session); err != nil { if err := ws.handler.SessionProfileChanged(session); err != nil {
ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session receiving started with an error") ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session profile changed with an error")
} else { } else {
ws.logger.Debug().Str("id", session.ID()).Msg("session receiving started") ws.logger.Debug().Str("id", session.ID()).Msg("session profile changed")
} }
}) })
ws.sessions.OnReceivingStopped(func(session types.Session) { ws.sessions.OnStateChanged(func(session types.Session) {
if err := ws.handler.SessionReceivingStopped(session); err != nil { if err := ws.handler.SessionStateChanged(session); err != nil {
ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session receiving stopped with an error") ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session state changed with an error")
} else { } else {
ws.logger.Debug().Str("id", session.ID()).Msg("session receiving stopped") ws.logger.Debug().Str("id", session.ID()).Msg("session state changed")
}
})
ws.sessions.OnProfileUpdated(func(session types.Session) {
if err := ws.handler.SessionProfileUpdated(session); err != nil {
ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session profile updated with an error")
} else {
ws.logger.Debug().Str("id", session.ID()).Msg("session profile updated")
} }
}) })