node.js Socket.io - 失败:在收到握手响应之前连接已关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26665840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:03:36  来源:igfitidea点击:

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

node.jswebsocketsocket.io

提问by Jean Meyer

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

NodeJS 的 Socket.io 似乎不能用作 websocket 服务器

For some reason, socket.io ALWAYS fallback to the long polling and if I force the websocket transport layer, it will error out:

出于某种原因,socket.io 总是回退到长轮询,如果我强制使用 websocket 传输层,它会出错:

failed: Connection closed before receiving a handshake response

失败:在收到握手响应之前连接已关闭

The right ports are open. I'm using the chat examplefrom socket.io. I set up the server on http://jmi.io:3000. As you can see, it works fine with a beautiful long polling channel but now try the websocket connection from a websocket client and...

正确的端口是开放的。我正在使用socket.io 中的聊天示例。我在http://jmi.io:3000上设置了服务器。如您所见,它在漂亮的长轮询通道上运行良好,但现在尝试从 websocket 客户端进行 websocket 连接,然后...

WebSocket connection to 'ws://jmi:3000/' failed: Connection closed before receiving a handshake response

与“ws://jmi:3000/”的 WebSocket 连接失败:在收到握手响应之前连接已关闭

I have only one node single threaded and have the exact same package.json than in the chat example repo.

我只有一个单线程节点,并且具有与聊天示例存储库中完全相同的 package.json。

{
  "name": "socket-chat-example",
  "version": "0.0.1",
  "description": "my first socket.io app",
  "dependencies": {
    "express": "4.3.1",
    "socket.io": "1.2.0"
  }
}

Thank you for your help

感谢您的帮助

回答by jetset

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.

我遇到了完全相同的问题,因为我定义了 'io' 两次。仔细检查您在代码中定义 io 的位置,并确保您没有两次定义变量 io。

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'));
});

回答by Andy

Slightly related to what @Lucas Klaassen answered: I had the following:

与@Lucas Klaassen 的回答略有关联:我有以下几点:

let express = require('express');
let app = express();
let http = require('http').Server(app);
let io = require('socket.io')(http);
// this is the culprit:
app.listen(port);

Changing last line is what fixed it:

更改最后一行是修复它的原因:

http.listen(port);

回答by Jamie Hutber

In my case the problem was that I am proxying the server through browser-sync

就我而言,问题是我通过代理服务器 browser-sync

browserSync({
    //server: {
    //  // src is included for use with sass source maps
    //  baseDir: ['public', 'src']
    //},
    proxy: "localhost:4045",
    port: 4046

So this will fail, however in production it works fine.

所以这会失败,但是在生产中它工作正常。

回答by xameeramir

Here is the complete working example from one of our working projects:

这是我们工作项目之一的完整工作示例:

const websocket = require('ws');
const http = require('http');
const express = require('express');
const app = express();
const server = http.createServer(app);
wss = new websocket.Server({ server });
app.on('upgrade', wss.handleUpgrade);

wss.on('connection', ws => {
  console.log('web socket connection is alive');
}

server.listen(8080, () => {
    console.log('server started on PORT 8080');
});

translated from Medium

翻译自媒体