From 27905a488ff535eed43d83c0183f8075ea792fde Mon Sep 17 00:00:00 2001 From: Thomas Miller Date: Sun, 22 Aug 2021 09:49:09 -0700 Subject: [PATCH 1/2] Add notification to the middleware of peer removal. --- pkg/sfu/datachannel.go | 5 +++++ pkg/sfu/subscriber.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/pkg/sfu/datachannel.go b/pkg/sfu/datachannel.go index f292a81f4..f145d68b3 100644 --- a/pkg/sfu/datachannel.go +++ b/pkg/sfu/datachannel.go @@ -14,6 +14,7 @@ type ( Label string middlewares []func(MessageProcessor) MessageProcessor onMessage func(ctx context.Context, args ProcessArgs) + onRemovePeer func( peer Peer ) } ProcessArgs struct { @@ -49,6 +50,10 @@ func (dc *Datachannel) OnMessage(fn func(ctx context.Context, args ProcessArgs)) dc.onMessage = fn } +func (dc *Datachannel) OnRemovePeer(fn func(peer Peer)) { + dc.onRemovePeer = fn +} + func (p ProcessFunc) Process(ctx context.Context, args ProcessArgs) { p(ctx, args) } diff --git a/pkg/sfu/subscriber.go b/pkg/sfu/subscriber.go index 63ea9e0c2..45d8c53ba 100644 --- a/pkg/sfu/subscriber.go +++ b/pkg/sfu/subscriber.go @@ -93,6 +93,11 @@ func (s *Subscriber) AddDatachannel(peer Peer, dc *Datachannel) error { DataChannel: ndc, }) }) + ndc.OnClose(func() { + if dc.onRemovePeer != nil { + dc.onRemovePeer( peer ) + } + }) s.channels[dc.Label] = ndc From 253755fe0d6e2cd0942909285568f20ec5d0f8a3 Mon Sep 17 00:00:00 2001 From: Thomas Miller Date: Sun, 22 Aug 2021 11:55:36 -0700 Subject: [PATCH 2/2] run gofmt --- pkg/sfu/datachannel.go | 8 ++++---- pkg/sfu/subscriber.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/sfu/datachannel.go b/pkg/sfu/datachannel.go index f145d68b3..497af0946 100644 --- a/pkg/sfu/datachannel.go +++ b/pkg/sfu/datachannel.go @@ -11,10 +11,10 @@ type ( // The datachannels created will be negotiated on join to all peers that joins // the SFU. Datachannel struct { - Label string - middlewares []func(MessageProcessor) MessageProcessor - onMessage func(ctx context.Context, args ProcessArgs) - onRemovePeer func( peer Peer ) + Label string + middlewares []func(MessageProcessor) MessageProcessor + onMessage func(ctx context.Context, args ProcessArgs) + onRemovePeer func(peer Peer) } ProcessArgs struct { diff --git a/pkg/sfu/subscriber.go b/pkg/sfu/subscriber.go index 45d8c53ba..ca002ce83 100644 --- a/pkg/sfu/subscriber.go +++ b/pkg/sfu/subscriber.go @@ -95,7 +95,7 @@ func (s *Subscriber) AddDatachannel(peer Peer, dc *Datachannel) error { }) ndc.OnClose(func() { if dc.onRemovePeer != nil { - dc.onRemovePeer( peer ) + dc.onRemovePeer(peer) } })