Skip to content

Instantly share code, notes, and snippets.

@makkes
Created March 25, 2014 13:40
Show Gist options
  • Save makkes/9762010 to your computer and use it in GitHub Desktop.
Save makkes/9762010 to your computer and use it in GitHub Desktop.
pc2 onopen fires non-deterministically
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);
});
@modeswitch
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment