简体   繁体   中英

Opening multiple websocket connections in single app

I need to perform a load testing on our websocket service. Is there a way to open multiple websocket connections from a single workstation?

I already tried npm ws and websocket modules to launch them with NodeJS. It works fine with a single connection, but when I try to open them in a for-loop it throws an exception or uses only last open client.

If you want to do it from a single workstation, you can use child processes :

app.js :

var cp = require('child_process');
var children = [];
var max = 10; // tweak this to whatever you want
for(var i = 0; i<max; i++){
    children[i] = cp.fork('./child.js');
    .on('error', function(e) {
        console.log(e);
    }).on('message',function(m){
        console.log(m)
    }).on('exit', function(code) {
      console.log('exit with code',code);
    });
}

then in child.js, just have a script that starts a connection. you can also send messages between parent and child, using the child process API

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM