Skip to content

Instantly share code, notes, and snippets.

@tsujio
Last active August 29, 2015 14:03
Show Gist options
  • Save tsujio/6640a7591de2b4783758 to your computer and use it in GitHub Desktop.
Save tsujio/6640a7591de2b4783758 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://cdn.peerjs.com/0.3.8/peer.js"></script>
<script type="text/javascript" src="./overload-peerjs.js"></script>
</head>
<body>
</body>
</html>
(function() {
var clients = {};
CLIENTS_NUM = 50;
for (var i = 0; i < CLIENTS_NUM; i++) {
var client = new Peer({
host: '',
port: 9000,
key: ''
});
client.on('open', function(id) {
clients[id] = client;
});
client.on('error', function(error) {
console.log(error);
});
client.on('connection', function(conn) {
conn.on('open', function() {
setupConn(this);
});
});
}
var timer = setInterval(function() {
if (Object.keys(clients).length < CLIENTS_NUM) {
return;
}
clearInterval(timer);
var keys = Object.keys(clients);
for (var i = 0; i < CLIENTS_NUM; i += 2) {
var conn = clients[keys[i]].connect(keys[i+1]);
conn.on('open', function() {
setupConn(this);
});
}
console.log("Created " + CLIENTS_NUM + " clients.");
});
var requests = {};
var setupConn = function(conn) {
setInterval(function() {
var request = {
id: Math.random().toString(),
type: 'request'
};
setTimeout(function() {
if (requests[request.id]) {
console.error("A response has not been returned.");
delete requests[request.id];
}
}, 10000);
requests[request.id] = request;
conn.send(request);
}, 1000);
conn.on('data', function(message) {
if (message.type === 'request') {
conn.send({
id: message.id,
type: 'response'
});
} else {
if (requests[message.id]) {
delete requests[message.id];
} else {
console.error("Unknown id:", message.id);
}
}
});
conn.on('error', function(error) {
console.log(error);
});
};
window.clients = clients;
window.requests = requests;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment