From fa5482309879d367bd30c984e7d3c322bbb615bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Wed, 25 Nov 2020 20:02:53 +0100 Subject: [PATCH] Split connected to WebRTC & WebSocket. --- internal/session/session.go | 39 ++++++++++++++++++++++++++++------- internal/types/session.go | 3 +++ internal/webrtc/manager.go | 2 ++ internal/websocket/manager.go | 4 ++++ 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/internal/session/session.go b/internal/session/session.go index 189aadea..ebf4bf0c 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -19,13 +19,16 @@ type MemberProfile struct { } type SessionCtx struct { - id string - logger zerolog.Logger - manager *SessionManagerCtx - profile MemberProfile - websocket_peer types.WebSocketPeer - webrtc_peer types.WebRTCPeer - connected bool + id string + logger zerolog.Logger + manager *SessionManagerCtx + profile MemberProfile + websocket_peer types.WebSocketPeer + websocket_connected bool + webrtc_peer types.WebRTCPeer + webrtc_connected bool + // TODO: Refactor. + connected bool } func (session *SessionCtx) ID() string { @@ -57,10 +60,31 @@ func (session *SessionCtx) SetWebSocketPeer(websocket_peer types.WebSocketPeer) session.manager.emmiter.Emit("created", session) } +func (session *SessionCtx) SetWebSocketConnected(connected bool) { + if connected { + session.websocket_connected = true + session.manager.emmiter.Emit("websocket_connected", session) + } else { + session.websocket_connected = false + session.manager.emmiter.Emit("websocket_disconnected", session) + } +} + func (session *SessionCtx) SetWebRTCPeer(webrtc_peer types.WebRTCPeer) { session.webrtc_peer = webrtc_peer } +func (session *SessionCtx) SetWebRTCConnected(connected bool) { + if connected { + session.webrtc_connected = true + session.manager.emmiter.Emit("webrtc_connected", session) + } else { + session.webrtc_connected = false + session.manager.emmiter.Emit("webrtc_disconnected", session) + } +} + +// TODO: Refactor. func (session *SessionCtx) SetConnected(connected bool) { session.connected = connected @@ -76,6 +100,7 @@ func (session *SessionCtx) SetConnected(connected bool) { } func (session *SessionCtx) Disconnect(reason string) error { + // TODO: Refactor. session.SetConnected(false) return session.Send( diff --git a/internal/types/session.go b/internal/types/session.go index d241cfbf..29507962 100644 --- a/internal/types/session.go +++ b/internal/types/session.go @@ -10,7 +10,10 @@ type Session interface { Connected() bool SetName(name string) SetWebSocketPeer(websocket_peer WebSocketPeer) + SetWebSocketConnected(connected bool) SetWebRTCPeer(webrtc_peer WebRTCPeer) + SetWebRTCConnected(connected bool) + // TODO: Refactor. SetConnected(connected bool) Disconnect(reason string) error Send(v interface{}) error diff --git a/internal/webrtc/manager.go b/internal/webrtc/manager.go index acce3009..9997ec03 100644 --- a/internal/webrtc/manager.go +++ b/internal/webrtc/manager.go @@ -156,9 +156,11 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (string, bool case webrtc.PeerConnectionStateDisconnected: case webrtc.PeerConnectionStateFailed: manager.logger.Info().Str("id", session.ID()).Msg("peer disconnected") + // TODO: Change WebRTC connection state. session.SetConnected(false) case webrtc.PeerConnectionStateConnected: manager.logger.Info().Str("id", session.ID()).Msg("peer connected") + // TODO: Change WebRTC connection state. session.SetConnected(true) } }) diff --git a/internal/websocket/manager.go b/internal/websocket/manager.go index 7b583173..b4a48f4b 100644 --- a/internal/websocket/manager.go +++ b/internal/websocket/manager.go @@ -187,9 +187,13 @@ func (ws *WebSocketManagerCtx) handle(connection *websocket.Conn, session types. ticker := time.NewTicker(pingPeriod) go func() { + // TODO: Change WebSocket connection state. + //session.SetConnected(true) + defer func() { ticker.Stop() ws.logger.Debug().Str("address", connection.RemoteAddr().String()).Msg("handle socket ending") + // TODO: Change WebSocket connection state. session.SetConnected(false) }()