Socket.io - failed: Connection closed before receiving a handshake response

前端 未结 5 1336
庸人自扰
庸人自扰 2021-01-01 15:24

Socket.io for NodeJS doesn\'t seem to work as a websocket server

For some reason, socket.io ALWAYS fallback to the long polling and if I force the w

5条回答
  •  迷失自我
    2021-01-01 16:09

    I had the exact same issue because I was defining 'io' twice. Double check where you are defining io in your code and ensure you are not defining the variable io twice.

    Example of what I was doing wrong:

    var server = require('http').createServer(app);
    var io = require('socket.io')(server);
    
    var io = require('socket.io').listen(server.listen(config.port, config.ip, function () {
        console.log('Express server listening on %d, in %s mode', config.port,     
        app.get('env'));
    }));
    

    Example of what fixed the issue:

    var server = require('http').createServer(app);
    var io = require('socket.io')(server);
    
    server.listen(config.port, config.ip, function () {
        console.log('Express server listening on %d, in %s mode', config.port, 
        app.get('env'));
    });
    

提交回复
热议问题