Connected / Disconnected refactor.

This commit is contained in:
Miroslav Šedivý 2020-11-01 21:03:25 +01:00
parent e51541fe01
commit 51c6e0091f
5 changed files with 17 additions and 18 deletions

View File

@ -168,7 +168,7 @@ func (manager *SessionManagerCtx) OnConnected(listener func(session types.Sessio
func (manager *SessionManagerCtx) OnDisconnected(listener func(session types.Session)) { func (manager *SessionManagerCtx) OnDisconnected(listener func(session types.Session)) {
manager.emmiter.On("disconnected", func(payload ...interface{}) { manager.emmiter.On("disconnected", func(payload ...interface{}) {
// Stop streaming, if everyone left // Stop streaming, if everyone left
if manager.capture.Streaming() && len(manager.members) == 0 { if manager.capture.Streaming() && len(manager.Members()) == 0 {
manager.capture.StopStream() manager.capture.StopStream()
} }

View File

@ -69,18 +69,18 @@ func (session *SessionCtx) SetPeer(peer types.Peer) {
session.peer = peer session.peer = peer
} }
func (session *SessionCtx) SetConnected() { func (session *SessionCtx) SetConnected(connected bool) {
session.connected = true session.connected = connected
session.manager.emmiter.Emit("connected", session)
}
func (session *SessionCtx) SetDisconnected() { if connected {
session.connected = false session.manager.emmiter.Emit("connected", session)
session.manager.emmiter.Emit("disconnected", session) } else {
session.socket = nil session.manager.emmiter.Emit("disconnected", session)
session.socket = nil
// TODO: Refactor. // TODO: Refactor.
session.manager.Destroy(session.id) session.manager.Destroy(session.id)
}
} }
func (session *SessionCtx) Disconnect(reason string) error { func (session *SessionCtx) Disconnect(reason string) error {
@ -96,7 +96,7 @@ func (session *SessionCtx) Disconnect(reason string) error {
return err return err
} }
session.SetDisconnected() session.SetConnected(false)
return nil return nil
} }

View File

@ -14,8 +14,7 @@ type Session interface {
SetName(name string) SetName(name string)
SetSocket(socket WebSocket) SetSocket(socket WebSocket)
SetPeer(peer Peer) SetPeer(peer Peer)
SetConnected() SetConnected(connected bool)
SetDisconnected()
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,10 +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")
session.SetDisconnected() 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")
session.SetConnected() session.SetConnected(true)
} }
}) })

View File

@ -197,7 +197,7 @@ func (ws *WebSocketManagerCtx) handle(connection *websocket.Conn, session types.
defer func() { defer func() {
ticker.Stop() ticker.Stop()
ws.logger.Debug().Str("address", connection.RemoteAddr().String()).Msg("handle socket ending") ws.logger.Debug().Str("address", connection.RemoteAddr().String()).Msg("handle socket ending")
session.SetDisconnected() session.SetConnected(false)
}() }()
for { for {