mirror of
https://github.com/pion/webrtc.git
synced 2025-10-05 07:06:51 +08:00
Adding OnDial handler for datachannels
Adds an `OnDial` handler to be fired once the dialing side has sent the DCEP OPEN message over the data channel.
This commit is contained in:
@@ -49,6 +49,8 @@ type DataChannel struct {
|
||||
onMessageHandler func(DataChannelMessage)
|
||||
openHandlerOnce sync.Once
|
||||
onOpenHandler func()
|
||||
dialHandlerOnce sync.Once
|
||||
onDialHandler func()
|
||||
onCloseHandler func()
|
||||
onBufferedAmountLow func()
|
||||
onErrorHandler func(error)
|
||||
@@ -175,6 +177,7 @@ func (d *DataChannel) open(sctpTransport *SCTPTransport) error {
|
||||
dc.OnBufferedAmountLow(d.onBufferedAmountLow)
|
||||
d.mu.Unlock()
|
||||
|
||||
d.onDial()
|
||||
d.handleOpen(dc, false, d.negotiated)
|
||||
return nil
|
||||
}
|
||||
@@ -228,6 +231,30 @@ func (d *DataChannel) onOpen() {
|
||||
}
|
||||
}
|
||||
|
||||
// OnDial sets an event handler which is invoked when the
|
||||
// peer has been dialed, but before said peer has responsed
|
||||
func (d *DataChannel) OnDial(f func()) {
|
||||
d.mu.Lock()
|
||||
d.dialHandlerOnce = sync.Once{}
|
||||
d.onDialHandler = f
|
||||
d.mu.Unlock()
|
||||
|
||||
if d.ReadyState() == DataChannelStateOpen {
|
||||
// If the data channel is already open, call the handler immediately.
|
||||
go d.dialHandlerOnce.Do(f)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DataChannel) onDial() {
|
||||
d.mu.RLock()
|
||||
handler := d.onDialHandler
|
||||
d.mu.RUnlock()
|
||||
|
||||
if handler != nil {
|
||||
go d.dialHandlerOnce.Do(handler)
|
||||
}
|
||||
}
|
||||
|
||||
// OnClose sets an event handler which is invoked when
|
||||
// the underlying data transport has been closed.
|
||||
func (d *DataChannel) OnClose(f func()) {
|
||||
|
Reference in New Issue
Block a user