我需要对我们的 websocket 服务执行负载测试。 有没有办法从单个工作站打开多个 websocket 连接?
我已经尝试过 npm ws
和 websocket
模块来使用 NodeJS 启动它们。它在单个连接上工作正常,但是当我尝试在 for 循环中打开它们时,它会抛出异常或仅使用最后一个打开的客户端。
最佳答案
如果您想从单个工作站执行此操作,可以使用子进程:
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);
});
}
然后在 child.js 中,只需一个启动连接的脚本。您还可以使用子进程 API 在父进程和子进程之间发送消息
关于javascript - 在单个应用程序中打开多个 websocket 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40864605/