Skip to content

Instantly share code, notes, and snippets.

@makryl
Last active February 28, 2017 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makryl/3d9b5068202e766d902616cb30c600fa to your computer and use it in GitHub Desktop.
Save makryl/3d9b5068202e766d902616cb30c600fa to your computer and use it in GitHub Desktop.
test node webrtc connections limit
var webrtc = require('..');
var RTCPeerConnection = webrtc.RTCPeerConnection;
var RTCSessionDescription = webrtc.RTCSessionDescription;
var RTCIceCandidate = webrtc.RTCIceCandidate;
var total = 0;
var limit = parseInt(process.argv[process.argv.length - 1]) || 1;
for (var i = 0; i < limit; i++) {
(function(i){
test(i);
}(i));
}
function test(i) {
console.log("create connections", i);
var pc1 = new RTCPeerConnection();
var pc2 = new RTCPeerConnection();
pc1.onicecandidate = function(candidate) {
if (candidate && candidate.candidate)
pc2.addIceCandidate(candidate.candidate);
}
pc2.onicecandidate = function(candidate) {
if (candidate && candidate.candidate)
pc1.addIceCandidate(candidate.candidate);
}
function handle_error(error)
{
throw error;
}
var checks = 0;
var expected = 2;
function create_data_channels() {
console.log("create data channels", i);
var dc1 = pc1.createDataChannel('test', {
ordered: false
});
dc1.onopen = function() {
console.log("pc1: data channel open", i);
if(++ checks == expected) {
done();
}
}
pc2.ondatachannel = function(event) {
var dc2 = event.channel;
dc2.onopen = function() {
console.log("pc2: data channel open", i);
if(++ checks == expected) {
done();
}
};
}
create_offer();
}
function create_offer() {
console.log('pc1: create offer', i);
pc1.createOffer(set_pc1_local_description, handle_error);
}
function set_pc1_local_description(desc) {
console.log('pc1: set local description', i);
pc1.setLocalDescription(
new RTCSessionDescription(desc),
set_pc2_remote_description.bind(undefined, desc),
handle_error
);
}
function set_pc2_remote_description(desc) {
console.log('pc2: set remote description', i);
pc2.setRemoteDescription(
new RTCSessionDescription(desc),
create_answer,
handle_error
);
}
function create_answer() {
console.log('pc2: create answer', i);
pc2.createAnswer(
set_pc2_local_description,
handle_error
);
}
function set_pc2_local_description(desc) {
console.log('pc2: set local description', i);
pc2.setLocalDescription(
new RTCSessionDescription(desc),
set_pc1_remote_description.bind(undefined, desc),
handle_error
);
}
function set_pc1_remote_description(desc) {
console.log('pc1: set remote description', i);
pc1.setRemoteDescription(
new RTCSessionDescription(desc),
wait,
handle_error
);
}
function wait() {
console.log('waiting', i);
}
function run() {
create_data_channels();
}
function done() {
//console.log('cleanup', i);
//pc1.close();
//pc2.close();
console.log('done %s (%s/%s)', i, ++total, limit);
}
run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment