diff --git a/pkg/session.go b/pkg/session.go index c7c60dbbb..a755b9134 100644 --- a/pkg/session.go +++ b/pkg/session.go @@ -95,6 +95,8 @@ func (s *Session) AddDatachannel(owner string, dc *webrtc.DataChannel) { dc.OnMessage(func(msg webrtc.DataChannelMessage) { s.onMessage(pid, label, msg) }) + + p.subscriber.negotiate() } } @@ -123,6 +125,7 @@ func (s *Session) Publish(router Router, rr *receiverRouter) { func (s *Session) Subscribe(peer *Peer) { s.mu.RLock() defer s.mu.RUnlock() + subdChans := false for pid, p := range s.peers { if pid == peer.id { continue @@ -132,6 +135,25 @@ func (s *Session) Subscribe(peer *Peer) { log.Errorf("Subscribing to router err: %v", err) continue } + + if !subdChans { + for _, dc := range p.subscriber.channels { + label := dc.Label() + new, err := peer.subscriber.AddDataChannel(label) + + if err != nil { + log.Errorf("error adding datachannel: %s", err) + continue + } + + new.OnMessage(func(msg webrtc.DataChannelMessage) { + s.onMessage(peer.id, label, msg) + }) + } + subdChans = true + + peer.subscriber.negotiate() + } } }