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)) {
manager.emmiter.On("receiving_started", func(payload ...interface{}) {
func (manager *SessionManagerCtx) OnProfileChanged(listener func(session types.Session)) {
manager.emmiter.On("profile_changed", func(payload ...interface{}) {
listener(payload[0].(*SessionCtx))
})
}
func (manager *SessionManagerCtx) OnReceivingStopped(listener func(session types.Session)) {
manager.emmiter.On("receiving_stopped", func(payload ...interface{}) {
listener(payload[0].(*SessionCtx))
})
}
func (manager *SessionManagerCtx) OnProfileUpdated(listener func(session types.Session)) {
manager.emmiter.On("profile_updated", func(payload ...interface{}) {
func (manager *SessionManagerCtx) OnStateChanged(listener func(session types.Session)) {
manager.emmiter.On("state_changed", func(payload ...interface{}) {
listener(payload[0].(*SessionCtx))
})
}

View File

@ -61,7 +61,7 @@ func (session *SessionCtx) CanAccessClipboard() bool {
func (session *SessionCtx) SetProfile(profile types.MemberProfile) {
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) {
if connected {
session.webrtc_connected = true
session.manager.emmiter.Emit("receiving_started", session)
} else {
session.webrtc_connected = false
session.manager.emmiter.Emit("receiving_stopped", session)
session.webrtc_connected = connected
session.manager.emmiter.Emit("state_changed", session)
if !connected {
session.webrtc_peer = nil
}
}

View File

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

View File

@ -66,12 +66,18 @@ type MemberProfile struct {
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 {
Event string `json:"event,omitempty"`
ID string `json:"id"`
Profile MemberProfile `json:"profile"`
IsConnected bool `json:"is_connected"`
IsReceiving bool `json:"is_receiving"`
State MemberState `json:"state"`
}
/////////////////////////////

View File

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

View File

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

View File

@ -10,9 +10,9 @@ func (h *MessageHandlerCtx) SessionCreated(session types.Session) error {
// TODO: Join structs?
h.sessions.Broadcast(
message.MemberData{
Event: event.MEMBER_CREATED,
ID: session.ID(),
Profile: message.MemberProfile{
Event: event.MEMBER_CREATED,
ID: session.ID(),
Profile: message.MemberProfile{
Name: session.Name(),
IsAdmin: session.IsAdmin(),
CanLogin: session.CanLogin(),
@ -21,8 +21,10 @@ func (h *MessageHandlerCtx) SessionCreated(session types.Session) error {
CanHost: session.CanHost(),
CanAccessClipboard: session.CanAccessClipboard(),
},
IsConnected: session.IsConnected(),
IsReceiving: session.IsReceiving(),
State: message.MemberState{
IsConnected: session.IsConnected(),
IsReceiving: session.IsReceiving(),
},
}, nil)
return nil
@ -90,31 +92,11 @@ func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
return nil
}
func (h *MessageHandlerCtx) SessionReceivingStarted(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 {
func (h *MessageHandlerCtx) SessionProfileChanged(session types.Session) error {
// TODO: Join structs?
h.sessions.Broadcast(
message.MemberProfile{
Event: event.MEMBER_PROFILE_UPDATED,
Event: event.MEMBER_PROFILE,
ID: session.ID(),
Name: session.Name(),
IsAdmin: session.IsAdmin(),
@ -127,3 +109,16 @@ func (h *MessageHandlerCtx) SessionProfileUpdated(session types.Session) error {
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

@ -27,8 +27,8 @@ func (h *MessageHandlerCtx) systemInit(session types.Session) error {
for _, session := range h.sessions.Members() {
// TODO: Join structs?
members[session.ID()] = message.MemberData{
ID: session.ID(),
Profile: message.MemberProfile{
ID: session.ID(),
Profile: message.MemberProfile{
Name: session.Name(),
IsAdmin: session.IsAdmin(),
CanLogin: session.CanLogin(),
@ -37,8 +37,10 @@ func (h *MessageHandlerCtx) systemInit(session types.Session) error {
CanHost: session.CanHost(),
CanAccessClipboard: session.CanAccessClipboard(),
},
IsConnected: session.IsConnected(),
IsReceiving: session.IsReceiving(),
State: message.MemberState{
IsConnected: session.IsConnected(),
IsReceiving: session.IsReceiving(),
},
}
}

View File

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