refactor WebRTC antipatterns.

This commit is contained in:
Miroslav Šedivý 2020-10-31 21:46:29 +01:00
parent b1b5a70c64
commit cca147859d
4 changed files with 17 additions and 23 deletions

View File

@ -48,6 +48,7 @@ func (session *Session) Address() string {
if session.socket == nil { if session.socket == nil {
return "" return ""
} }
return session.socket.Address() return session.socket.Address()
} }
@ -64,26 +65,21 @@ func (session *Session) SetMuted(muted bool) {
session.muted = muted session.muted = muted
} }
func (session *Session) SetName(name string) error { func (session *Session) SetName(name string) {
session.name = name session.name = name
return nil
} }
func (session *Session) SetSocket(socket types.WebSocket) error { func (session *Session) SetSocket(socket types.WebSocket) {
session.socket = socket session.socket = socket
return nil
} }
func (session *Session) SetPeer(peer types.Peer) error { func (session *Session) SetPeer(peer types.Peer) {
session.peer = peer session.peer = peer
return nil
} }
func (session *Session) SetConnected(connected bool) { func (session *Session) SetConnected() {
session.connected = connected session.connected = true
if connected { session.manager.emmiter.Emit("connected", session.id, session)
session.manager.emmiter.Emit("connected", session.id, session)
}
} }
func (session *Session) Disconnect(reason string) error { func (session *Session) Disconnect(reason string) error {
@ -105,6 +101,7 @@ func (session *Session) Send(v interface{}) error {
if session.socket == nil { if session.socket == nil {
return nil return nil
} }
return session.socket.Send(v) return session.socket.Send(v)
} }
@ -112,6 +109,7 @@ func (session *Session) SignalAnswer(sdp string) error {
if session.peer == nil { if session.peer == nil {
return nil return nil
} }
return session.peer.SignalAnswer(sdp) return session.peer.SignalAnswer(sdp)
} }

View File

@ -16,10 +16,10 @@ type Session interface {
Connected() bool Connected() bool
Member() *Member Member() *Member
SetMuted(muted bool) SetMuted(muted bool)
SetName(name string) error SetName(name string)
SetConnected(connected bool) SetConnected()
SetSocket(socket WebSocket) error SetSocket(socket WebSocket)
SetPeer(peer Peer) error SetPeer(peer Peer)
Address() string Address() string
Disconnect(message string) error Disconnect(message string) error
Send(v interface{}) error Send(v interface{}) error

View File

@ -152,11 +152,11 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri
session.Disconnect("peer connection state failed") session.Disconnect("peer connection state failed")
case webrtc.PeerConnectionStateConnected: case webrtc.PeerConnectionStateConnected:
manager.logger.Info().Str("id", id).Msg("peer connected") manager.logger.Info().Str("id", id).Msg("peer connected")
session.SetConnected(true) session.SetConnected()
} }
}) })
if err := session.SetPeer(&Peer{ session.SetPeer(&Peer{
id: id, id: id,
api: api, api: api,
engine: &engine, engine: &engine,
@ -164,9 +164,7 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri
settings: &settings, settings: &settings,
connection: connection, connection: connection,
configuration: configuration, configuration: configuration,
}); err != nil { })
return "", manager.config.ICELite, manager.config.ICEServers, err
}
return description.SDP, manager.config.ICELite, manager.config.ICEServers, nil return description.SDP, manager.config.ICELite, manager.config.ICEServers, nil
} }

View File

@ -26,9 +26,7 @@ func (h *MessageHandler) signalProvide(id string, session types.Session) error {
} }
func (h *MessageHandler) signalAnswer(id string, session types.Session, payload *message.SignalAnswer) error { func (h *MessageHandler) signalAnswer(id string, session types.Session, payload *message.SignalAnswer) error {
if err := session.SetName(payload.DisplayName); err != nil { session.SetName(payload.DisplayName)
return err
}
if err := session.SignalAnswer(payload.SDP); err != nil { if err := session.SignalAnswer(payload.SDP); err != nil {
return err return err