Skip to content

Commit

Permalink
Add support for negotiated channels.
Browse files Browse the repository at this point in the history
  • Loading branch information
butmitchtho committed Nov 25, 2019
1 parent c12a726 commit 9663499
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Peer extends stream.Duplex {

this.initiator = opts.initiator || false
this.channelConfig = opts.channelConfig || Peer.channelConfig
this.negotiated = this.channelConfig.negotiated
this.config = Object.assign({}, Peer.config, opts.config)
this.offerOptions = opts.offerOptions || {}
this.answerOptions = opts.answerOptions || {}
Expand Down Expand Up @@ -83,7 +84,7 @@ class Peer extends stream.Duplex {
this._channel = null
this._pendingCandidates = []

this._isNegotiating = !this.initiator // is this peer waiting for negotiation to complete?
this._isNegotiating = this.negotiated ? false : !this.initiator // is this peer waiting for negotiation to complete?
this._batchedNegotiation = false // batch synchronous negotiations
this._queuedNegotiation = false // is there a queued negotiation request?
this._sendersAwaitingStable = []
Expand Down Expand Up @@ -131,7 +132,7 @@ class Peer extends stream.Duplex {
// - onfingerprintfailure
// - onnegotiationneeded

if (this.initiator) {
if (this.initiator || this.negotiated) {
this._setupData({
channel: this._pc.createDataChannel(this.channelName, this.channelConfig)
})
Expand Down
32 changes: 32 additions & 0 deletions test/negotiation.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,35 @@ test('add stream on non-initiator only', function (t) {
t.pass('peer1 got stream')
})
})

test('negotiated channels', function (t) {
t.plan(2)

var peer1 = new Peer({
config: config,
initiator: true,
wrtc: common.wrtc,
channelConfig: {
id: 1,
negotiated: true
}
})
var peer2 = new Peer({
config: config,
wrtc: common.wrtc,
channelConfig: {
id: 1,
negotiated: true
}
})

peer1.on('signal', function (data) { if (!peer2.destroyed) peer2.signal(data) })
peer2.on('signal', function (data) { if (!peer1.destroyed) peer1.signal(data) })

peer1.on('connect', function () {
t.pass('peer1 connect')
})
peer2.on('connect', function () {
t.pass('peer2 connect')
})
})

0 comments on commit 9663499

Please sign in to comment.