我尝试使用socket.io
在我的node
应用程序中实现cluster
。 如果我不使用cluster
一切正常。 但是当我使用cluster
时,它会在客户端浏览器中出现以下错误。
WebSocket connection to 'ws://localhost:8000/socket.io/?EIO=3&transport=websocket&sid=Ff8LkaCbF5g92lKOAAAS' failed: Error during WebSocket
socket.js:2 POST http://localhost:8000/socket.io/?EIO=3&transport=polling&t=MAjySbD&sid=Ff8LkaCbF5g92lKOAAAS 400 (Bad Request)
这是服务器.js
var http = require('http');
var app = require('../app');
cluster = module.exports = require('cluster');
const numCPUs = require('os').cpus().length;
var server = http.createServer(app);
io = module.exports = require('socket.io').listen(server, {
pingTimeout: 7000,
pingInterval: 10000
});
io.set("transports", ["xhr-polling","websocket","polling"]);
if(cluster.isMaster){
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
var port = 8000;
var host = '0.0.0.0';
server.listen(port,host,function(){
log('server is running on ' + host +':'+port);
});
}
这是客户端.js
var socket = io.connect('http://localhost:8000/dashboard',{transports: ['websocket']});
检查您是否在 Express 上使用 express-status-monitor
作为中间件,这使得 WebSocket 第一次(请求(握手时的 http 调用失败,或者可能是另一个因素,如代理 (nginx( 或类似因素
有关此错误的更多详细信息,请查看此处