Created
August 24, 2014 22:56
-
-
Save nulltask/6cb6cc00baa3f310eb67 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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