Split connected to WebRTC & WebSocket.

This commit is contained in:
Miroslav Šedivý 2020-11-25 20:02:53 +01:00
parent bc77c8f8f5
commit fa54823098
4 changed files with 41 additions and 7 deletions

View File

@ -24,7 +24,10 @@ type SessionCtx struct {
manager *SessionManagerCtx
profile MemberProfile
websocket_peer types.WebSocketPeer
websocket_connected bool
webrtc_peer types.WebRTCPeer
webrtc_connected bool
// TODO: Refactor.
connected bool
}
@ -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(

View File

@ -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

View File

@ -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)
}
})

View File

@ -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)
}()