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

View File

@ -16,10 +16,10 @@ type Session interface {
Connected() bool
Member() *Member
SetMuted(muted bool)
SetName(name string) error
SetConnected(connected bool)
SetSocket(socket WebSocket) error
SetPeer(peer Peer) error
SetName(name string)
SetConnected()
SetSocket(socket WebSocket)
SetPeer(peer Peer)
Address() string
Disconnect(message string) 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")
case webrtc.PeerConnectionStateConnected:
manager.logger.Info().Str("id", id).Msg("peer connected")
session.SetConnected(true)
session.SetConnected()
}
})
if err := session.SetPeer(&Peer{
session.SetPeer(&Peer{
id: id,
api: api,
engine: &engine,
@ -164,9 +164,7 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri
settings: &settings,
connection: connection,
configuration: configuration,
}); err != nil {
return "", manager.config.ICELite, manager.config.ICEServers, err
}
})
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 {
if err := session.SetName(payload.DisplayName); err != nil {
return err
}
session.SetName(payload.DisplayName)
if err := session.SignalAnswer(payload.SDP); err != nil {
return err