session refactor state.

This commit is contained in:
Miroslav Šedivý 2021-02-26 15:04:50 +01:00
parent 93f8d7f05e
commit 09625ecca5
2 changed files with 7 additions and 17 deletions

View File

@ -13,10 +13,9 @@ type SessionCtx struct {
logger zerolog.Logger logger zerolog.Logger
manager *SessionManagerCtx manager *SessionManagerCtx
profile types.MemberProfile profile types.MemberProfile
state types.MemberState
websocketPeer types.WebSocketPeer websocketPeer types.WebSocketPeer
websocketConnected bool
webrtcPeer types.WebRTCPeer webrtcPeer types.WebRTCPeer
webrtcConnected bool
} }
func (session *SessionCtx) ID() string { func (session *SessionCtx) ID() string {
@ -76,13 +75,13 @@ func (session *SessionCtx) profileChanged() {
}, nil) }, nil)
} }
if !session.CanWatch() && session.IsWatching() { if !session.CanWatch() && session.state.IsWatching {
if err := session.webrtcPeer.Destroy(); err != nil { if err := session.webrtcPeer.Destroy(); err != nil {
session.logger.Warn().Err(err).Msgf("webrtc destroy has failed") session.logger.Warn().Err(err).Msgf("webrtc destroy has failed")
} }
} }
if (!session.CanConnect() || !session.CanLogin()) && session.IsConnected() { if (!session.CanConnect() || !session.CanLogin()) && session.state.IsConnected {
if err := session.Disconnect("profile changed"); err != nil { if err := session.Disconnect("profile changed"); err != nil {
session.logger.Warn().Err(err).Msgf("websocket destroy has failed") session.logger.Warn().Err(err).Msgf("websocket destroy has failed")
} }
@ -98,19 +97,11 @@ func (session *SessionCtx) IsHost() bool {
} }
func (session *SessionCtx) IsConnected() bool { func (session *SessionCtx) IsConnected() bool {
return session.websocketConnected return session.state.IsConnected
}
func (session *SessionCtx) IsWatching() bool {
return session.webrtcConnected
} }
func (session *SessionCtx) GetState() types.MemberState { func (session *SessionCtx) GetState() types.MemberState {
// TODO: Save state in member struct. return session.state
return types.MemberState{
IsConnected: session.IsConnected(),
IsWatching: session.IsWatching(),
}
} }
// --- // ---
@ -128,7 +119,7 @@ func (session *SessionCtx) SetWebSocketPeer(websocketPeer types.WebSocketPeer) {
} }
func (session *SessionCtx) SetWebSocketConnected(connected bool) { func (session *SessionCtx) SetWebSocketConnected(connected bool) {
session.websocketConnected = connected session.state.IsConnected = connected
if connected { if connected {
session.manager.emmiter.Emit("connected", session) session.manager.emmiter.Emit("connected", session)
@ -196,7 +187,7 @@ func (session *SessionCtx) SetWebRTCConnected(webrtcPeer types.WebRTCPeer, conne
return return
} }
session.webrtcConnected = connected session.state.IsWatching = connected
session.manager.emmiter.Emit("state_changed", session) session.manager.emmiter.Emit("state_changed", session)
if !connected { if !connected {

View File

@ -45,7 +45,6 @@ type Session interface {
// state // state
IsHost() bool IsHost() bool
IsConnected() bool IsConnected() bool
IsWatching() bool
GetState() MemberState GetState() MemberState
// websocket // websocket