Last active
August 29, 2015 14:07
-
-
Save makkes/44675a0ef9c61aa88283 to your computer and use it in GitHub Desktop.
wrtc DataChannel test
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 wrtc = require('wrtc'); | |
var pcs = [], | |
done = [], | |
MAX_PCS = 30; | |
pcoptions = { | |
iceServers: [] | |
}; | |
for (var i = 0; i < MAX_PCS; i += 2) { | |
pcs.push(new wrtc.RTCPeerConnection(pcoptions)); | |
pcs.push(new wrtc.RTCPeerConnection(pcoptions)); | |
connect(done, MAX_PCS, doneCallback, pcs[i], i, pcs[i + 1], i + 1); | |
} | |
function doneCallback() { | |
console.log("done"); | |
if (typeof process.exit === 'function') { | |
process.exit(); | |
} | |
} | |
function connect(done, expectedDone, doneCallback, pc1, idx1, pc2, idx2) { | |
var dc1 = pc1.createDataChannel(null, {}); | |
dc1.onopen = function(ev) { | |
dc1.onmessage = function(msg) { | |
done.push(idx1, idx2); | |
console.log("done " + done.length / 2 + " times (" + idx1 + " -> " + idx2 + ")"); | |
if (done.length === expectedDone) { | |
doneCallback(); | |
} | |
}; | |
dc1.send("[" + idx1 + "] ping"); | |
}; | |
pc1.createOffer(function(offer) { | |
pc1.setLocalDescription(offer); | |
pc1.onicecandidate = function(ev) { | |
if (pc1.iceGatheringState === 'complete') { | |
pc1.onicecandidate = function() {}; | |
pc1.createOffer(function(offer2) { | |
pc2.setRemoteDescription(offer2, function() {}, function(err) { | |
console.log("Error", err); | |
}); | |
pc2.createAnswer(function(answer) { | |
pc2.setLocalDescription(answer); | |
pc2.onicecandidate = function(ev) { | |
if (pc2.iceGatheringState === 'complete') { | |
pc2.onicecandidate = function() {}; | |
pc2.createAnswer(function(answer2) { | |
pc2.ondatachannel = function(ev) { | |
var dc2 = ev.channel; | |
dc2.onmessage = function(msg) { | |
dc2.send("[" + idx2 + "] pong"); | |
}; | |
}; | |
pc1.setRemoteDescription(answer2, function() {}, function(err) { | |
console.log("Error", err); | |
}); | |
}, function(err) { | |
console.log("Error creating 2nd answer", err); | |
}); | |
} | |
}; | |
}, function(err) { | |
console.log("Error creating 1st answer", err); | |
}); | |
}, function(err) { | |
console.log("Error creating 2nd offer", err); | |
}); | |
} | |
}; | |
}, function(err) { | |
console.log("Error creating 1st offer", err); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment