Skip to content

Instantly share code, notes, and snippets.

@tsujio
Created June 27, 2014 11:42
Show Gist options
  • Save tsujio/bba899679e176b5b02ab to your computer and use it in GitHub Desktop.
Save tsujio/bba899679e176b5b02ab to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="./overload-webrtc.js"></script>
</head>
<body>
</body>
</html>
(function() {
var RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription;
var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var RTCIceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate;
var Client = function(config, signalingChannel) {
var self = this;
this.id = Math.random().toString(32);
this.remoteId = null;
this._pc = new RTCPeerConnection(config);
this._channel = null;
this._signalingChannel = signalingChannel;
this._requests = {};
this._pc.onicecandidate = function(evt) {
if (evt.candidate)
self._signalingChannel.send(self.id, self.remoteId, JSON.stringify({ "candidate": evt.candidate }));
};
this._pc.onnegotiationneeded = function() {
self._pc.createOffer(function(desc) {
self.localDescCreated(desc);
}, function(error) {
console.error("Failed to create offer:", error);
});
};
};
Client.prototype = {
connect: function(remoteId) {
this.remoteId = remoteId;
this._channel = this._pc.createDataChannel(Math.random().toString(32));
this.setupChannel();
},
localDescCreated: function(desc) {
var self = this;
this._pc.setLocalDescription(desc, function() {
self._signalingChannel.send(self.id, self.remoteId, JSON.stringify({ "sdp": self._pc.localDescription }));
}, function(error) {
console.error("Failed to set local description:", error);
});
},
onsignalingmessage: function(remoteId, json) {
var self = this;
if (!this.remoteId) {
this.remoteId = remoteId;
this._pc.ondatachannel = function(evt) {
self._channel = evt.channel;
self.setupChannel();
};
}
var message = JSON.parse(json);
if (message.sdp) {
this._pc.setRemoteDescription(new RTCSessionDescription(message.sdp), function() {
if (self._pc.remoteDescription.type == "offer") {
self._pc.createAnswer(function(desc) {
self.localDescCreated(desc);
}, function(error) {
console.error("Failed to create answer:", error);
});
}
}, function(error) {
console.error("Failed to set remote description:", error);
});
} else {
this._pc.addIceCandidate(new RTCIceCandidate(message.candidate),
function() {}, function(error) {
console.error("Failed to add ice candidate:", error);
});
}
},
setupChannel: function() {
var self = this;
this._channel.onopen = function() {
setInterval(function() {
self.sendRequest();
}, 1000);
};
this._channel.onmessage = function(evt) {
var message = JSON.parse(evt.data);
if (message.type === 'request') {
self._channel.send(JSON.stringify({
id: message.id,
type: 'response'
}));
} else {
if (self._requests[message.id]) {
delete self._requests[message.id];
} else {
console.error("Unknown id:", message.id);
}
}
};
},
sendRequest: function() {
var self = this;
var request = {
id: Math.random().toString(),
type: 'request'
};
setTimeout(function() {
if (self._requests[request.id]) {
console.error("A response has not been returned.");
delete self._requests[request.id];
}
}, 10000);
this._requests[request.id] = request;
this._channel.send(JSON.stringify(request));
}
};
var clients = {};
var signalingChannel = {
send: function(from, to, message) {
clients[to].onsignalingmessage(from, message);
}
};
CLIENTS_NUM = 100;
for (var i = 0; i < CLIENTS_NUM; i++) {
var client = new Client({'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }]},
signalingChannel);
clients[client.id] = client;
}
var keys = Object.keys(clients);
for (var i = 0; i < CLIENTS_NUM; i += 2) {
clients[keys[i]].connect(keys[i+1]);
}
console.log("Created " + CLIENTS_NUM + " clients.");
window.clients = clients;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment