replace SetConnected function.

This commit is contained in:
Miroslav Šedivý 2020-11-25 20:26:11 +01:00
parent fa54823098
commit f9b2460c3b
5 changed files with 27 additions and 38 deletions

View File

@ -47,7 +47,6 @@ func (manager *SessionManagerCtx) New(id string, admin bool) types.Session {
profile: MemberProfile{ profile: MemberProfile{
is_admin: admin, is_admin: admin,
}, },
connected: false,
} }
manager.members[id] = session manager.members[id] = session

View File

@ -27,8 +27,6 @@ type SessionCtx struct {
websocket_connected bool websocket_connected bool
webrtc_peer types.WebRTCPeer webrtc_peer types.WebRTCPeer
webrtc_connected bool webrtc_connected bool
// TODO: Refactor.
connected bool
} }
func (session *SessionCtx) ID() string { func (session *SessionCtx) ID() string {
@ -48,7 +46,7 @@ func (session *SessionCtx) IsHost() bool {
} }
func (session *SessionCtx) Connected() bool { func (session *SessionCtx) Connected() bool {
return session.connected return session.websocket_connected && session.webrtc_connected
} }
func (session *SessionCtx) SetName(name string) { func (session *SessionCtx) SetName(name string) {
@ -66,7 +64,15 @@ func (session *SessionCtx) SetWebSocketConnected(connected bool) {
session.manager.emmiter.Emit("websocket_connected", session) session.manager.emmiter.Emit("websocket_connected", session)
} else { } else {
session.websocket_connected = false session.websocket_connected = false
session.manager.emmiter.Emit("websocket_disconnected", session)
// TODO: Refactor.
//session.manager.emmiter.Emit("websocket_disconnected", session)
session.manager.emmiter.Emit("disconnected", session)
session.websocket_peer = nil
// TODO: Refactor.
_ = session.manager.Destroy(session.id)
} }
} }
@ -77,31 +83,21 @@ func (session *SessionCtx) SetWebRTCPeer(webrtc_peer types.WebRTCPeer) {
func (session *SessionCtx) SetWebRTCConnected(connected bool) { func (session *SessionCtx) SetWebRTCConnected(connected bool) {
if connected { if connected {
session.webrtc_connected = true session.webrtc_connected = true
session.manager.emmiter.Emit("webrtc_connected", session)
// TODO: Refactor.
//session.manager.emmiter.Emit("webrtc_connected", session)
session.manager.emmiter.Emit("connected", session)
} else { } else {
session.webrtc_connected = false session.webrtc_connected = false
session.manager.emmiter.Emit("webrtc_disconnected", session) session.manager.emmiter.Emit("webrtc_disconnected", session)
}
}
// TODO: Refactor. session.webrtc_peer = nil
func (session *SessionCtx) SetConnected(connected bool) {
session.connected = connected
if connected {
session.manager.emmiter.Emit("connected", session)
} else {
session.manager.emmiter.Emit("disconnected", session)
session.websocket_peer = nil
// TODO: Refactor.
_ = session.manager.Destroy(session.id)
} }
} }
func (session *SessionCtx) Disconnect(reason string) error { func (session *SessionCtx) Disconnect(reason string) error {
// TODO: Refactor. // TODO: End WebSocket connection.
session.SetConnected(false) // TODO: End WebRTC connection.
return session.Send( return session.Send(
message.Disconnect{ message.Disconnect{

View File

@ -13,8 +13,6 @@ type Session interface {
SetWebSocketConnected(connected bool) SetWebSocketConnected(connected bool)
SetWebRTCPeer(webrtc_peer WebRTCPeer) SetWebRTCPeer(webrtc_peer WebRTCPeer)
SetWebRTCConnected(connected bool) SetWebRTCConnected(connected bool)
// TODO: Refactor.
SetConnected(connected bool)
Disconnect(reason string) error Disconnect(reason string) error
Send(v interface{}) error Send(v interface{}) error
SignalAnswer(sdp string) error SignalAnswer(sdp string) error

View File

@ -156,12 +156,10 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (string, bool
case webrtc.PeerConnectionStateDisconnected: case webrtc.PeerConnectionStateDisconnected:
case webrtc.PeerConnectionStateFailed: case webrtc.PeerConnectionStateFailed:
manager.logger.Info().Str("id", session.ID()).Msg("peer disconnected") manager.logger.Info().Str("id", session.ID()).Msg("peer disconnected")
// TODO: Change WebRTC connection state. session.SetWebRTCConnected(false)
session.SetConnected(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")
// TODO: Change WebRTC connection state. session.SetWebRTCConnected(true)
session.SetConnected(true)
} }
}) })

View File

@ -169,12 +169,16 @@ func (ws *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request) e
Str("address", connection.RemoteAddr().String()). Str("address", connection.RemoteAddr().String()).
Msg("connection started") Msg("connection started")
session.SetWebSocketConnected(true)
defer func() { defer func() {
ws.logger. ws.logger.
Debug(). Debug().
Str("session", session.ID()). Str("session", session.ID()).
Str("address", connection.RemoteAddr().String()). Str("address", connection.RemoteAddr().String()).
Msg("connection ended") Msg("connection ended")
session.SetWebSocketConnected(false)
}() }()
ws.handle(connection, session) ws.handle(connection, session)
@ -186,17 +190,11 @@ func (ws *WebSocketManagerCtx) handle(connection *websocket.Conn, session types.
cancel := make(chan struct{}) cancel := make(chan struct{})
ticker := time.NewTicker(pingPeriod) ticker := time.NewTicker(pingPeriod)
defer func() {
ticker.Stop()
}()
go func() { 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)
}()
for { for {
_, raw, err := connection.ReadMessage() _, raw, err := connection.ReadMessage()
if err != nil { if err != nil {