Skip to content

Instantly share code, notes, and snippets.

@rangercyh
Last active August 29, 2015 14:21
Show Gist options
  • Save rangercyh/ff5d996cdad5b2643b9c to your computer and use it in GitHub Desktop.
Save rangercyh/ff5d996cdad5b2643b9c to your computer and use it in GitHub Desktop.
ws_client
var WS = require('ws');
var cli = [];
function send() {
var str = 'I am ok!';
var len = Buffer.byteLength(str);
var buf = new Buffer(len + 2);
buf.writeUInt16LE(len, 0);
buf.write(str, 2, len);
this.send(buf);
}
function createCli() {
if (cli.length >= 1000) {
return;
}
var client = new WS('ws://10.20.68.102:4000');
client.on('open', function() {
cli.push(client);
});
client.on('close', function() {
console.log('closed');
});
}
function allSend() {
var nu = 0;
for (var i = 0; i < cli.length; ++i) {
send.call(cli[i]);
++nu;
}
console.log('send = ', nu);
}
setInterval(allSend, 500);
setInterval(createCli, 500);
var Ws = require('ws').Server;
var num = 0;
var server = new Ws({ port: 4000 });
server.on('connection', function(client) {
client.addEventListener('message', function(msg) {
// no thing to do
});
++num;
console.log('new comer = ', num);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment