webSocket握手意外响应代码期间出错:400

时间:2018-04-10 05:15:52

标签: javascript node.js websocket socket.io mean-stack

我尝试使用clusternode应用中实施socket.io。如果我不使用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)

这是server.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);
    });
}

这是client.js

var socket = io.connect('http://localhost:8000/dashboard',{transports: ['websocket']}); 

1 个答案:

答案 0 :(得分:1)

检查是否在快递上使用express-status-monitor作为中间件,这会使WebSocket第一次(请求)握手的http调用失败,或者可能是诸如代理(nginx)之类的其他因素或类似的情况

查看here以获得有关此错误的更多详细信息