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)) {
manager.emmiter.On("disconnected", func(payload ...interface{}) {
// Stop streaming, if everyone left
if manager.capture.Streaming() && len(manager.members) == 0 {
if manager.capture.Streaming() && len(manager.Members()) == 0 {
manager.capture.StopStream()
}

View File

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

View File

@ -14,8 +14,7 @@ type Session interface {
SetName(name string)
SetSocket(socket WebSocket)
SetPeer(peer Peer)
SetConnected()
SetDisconnected()
SetConnected(connected bool)
Disconnect(reason string) error
Send(v interface{}) 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.PeerConnectionStateFailed:
manager.logger.Info().Str("id", session.ID()).Msg("peer disconnected")
session.SetDisconnected()
session.SetConnected(false)
case webrtc.PeerConnectionStateConnected:
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() {
ticker.Stop()
ws.logger.Debug().Str("address", connection.RemoteAddr().String()).Msg("handle socket ending")
session.SetDisconnected()
session.SetConnected(false)
}()
for {