Skip to content

Instantly share code, notes, and snippets.

@nulltask
Created August 24, 2014 22:56
Show Gist options
  • Select an option

  • Save nulltask/6cb6cc00baa3f310eb67 to your computer and use it in GitHub Desktop.

Select an option

Save nulltask/6cb6cc00baa3f310eb67 to your computer and use it in GitHub Desktop.
var ioc = require('socket.io-client');
var timers = [];
var clients = [];
var client = require('node-serf').connect({ port: 7373 }, function() {
client.stream({ Type: 'user:socket.io' }, function(res) {
console.log(require('util').inspect(res));
try {
var payload = JSON.parse(res.Payload);
console.log(payload);
if (payload.type == 'connect') {
for (var i = 0; i < payload.connection; ++i) {
createConnection(payload.host, payload.roundtrip);
}
}
if (payload.type == 'disconnect') {
disconnect();
}
} catch (e) {
console.log(e.name, e.stack);
}
});
});
function createConnection(host, roundtrip) {
var timer = setTimeout(function() {
clients.push(ioc(host, { forceNew: true }));
}, Math.random() * roundtrip);
timers.push(timer);
}
function disconnect() {
timers.forEach(function(timer) {
console.log('clear timer', timer);
clearTimeout(timer);
});
timer = [];
clients.forEach(function(client) {
console.log('disconnect');
client.disconnect();
});
clients = [];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment