简体   繁体   English

在单个应用程序中打开多个Websocket连接

[英]Opening multiple websocket connections in single app

I need to perform a load testing on our websocket service. 我需要对我们的websocket服务执行负载测试。 Is there a way to open multiple websocket connections from a single workstation? 有没有办法从单个工作站打开多个websocket连接?

I already tried npm ws and websocket modules to launch them with NodeJS. 我已经尝试过npm wswebsocket模块来用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. 单个连接就可以正常工作,但是当我尝试在for循环中打开它们时,它将引发异常或仅使用最后一个打开的客户端。

If you want to do it from a single workstation, you can use child processes : 如果要从单个工作站执行此操作,则可以使用子进程:

app.js : 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. 然后在child.js中,只有启动连接的脚本。 you can also send messages between parent and child, using the child process API 您还可以使用子流程API在父子之间发送消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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