Created
March 25, 2014 13:40
-
-
Save makkes/9762010 to your computer and use it in GitHub Desktop.
pc2 onopen fires non-deterministically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var RTCPeerConnection = require('wrtc').RTCPeerConnection; | |
var RTCIceCandidate = require('wrtc').RTCIceCandidate; | |
var RTCPeerConnectionIceEvent = require('wrtc').RTCPeerConnectionIceEvent; | |
var pc1 = new RTCPeerConnection(null); | |
var pc2 = new RTCPeerConnection(null); | |
var channel = pc1.createDataChannel(null, {}); | |
channel.onopen = function(ev) { | |
console.log('open'); | |
}; | |
pc2.ondatachannel = function(ev) { | |
console.log('pc2 on data channel'); | |
ev.channel.onopen = function(ev2) { | |
console.log('dc2 open'); | |
setTimeout(function() { | |
pc1.close(); | |
pc2.close(); | |
}, 500); | |
}; | |
}; | |
function waitForPcGatheringComplete(pc) { | |
setTimeout(function() { | |
if(pc.iceGatheringState === "complete") { | |
pc.onicecandidate(new RTCPeerConnectionIceEvent('icecandidate', {candidate: null})); | |
} else { | |
waitForPcGatheringComplete(pc); | |
} | |
}, 100); | |
} | |
pc1.createOffer(function(offer) { | |
pc1.setLocalDescription(offer); | |
pc1.onicecandidate = function(ev) { | |
if(pc1.iceGatheringState === "complete" || ev.candidate === null) { | |
pc1.onicecandidate = null; | |
pc2.setRemoteDescription(pc1.localDescription); | |
pc2.createAnswer(function(answer) { | |
pc2.setLocalDescription(answer); | |
pc2.onicecandidate = function(ev2) { | |
if(pc2.iceGatheringState === "complete" || ev2.candidate === null) { | |
pc2.onicecandidate = null; | |
pc1.setRemoteDescription(pc2.localDescription); | |
} | |
}; | |
waitForPcGatheringComplete(pc2); | |
}); | |
} | |
}; | |
waitForPcGatheringComplete(pc1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you looked at the bridge/peer example I wrote? It does basically this. Run bridge.js in node and open peer.html in a browser.