WebSocket握手期间出错:缺少“连接"标头 [英] Error during WebSocket handshake: 'Connection' header is missing

查看:341
本文介绍了WebSocket握手期间出错:缺少“连接"标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Node的新手,甚至是Socket.io的新手.我刚刚在spaiglas.com上获得了我的第一个聊天应用程序,尽管开发人员控制台中的一个错误非常简单,但该应用程序仍然可以正常工作...

I'm completely new to Node, and even newer to Socket.io. I just got my first chat application online at spaiglas.com, which works despite an error in the developer console that is fairly straightforward...

WebSocket connection to 'ws://example.com/...' failed
: Error during WebSocket handshake
: 'Connection' header is missing

WebSocket connection to 'ws://example.com/...' failed
: Error during WebSocket handshake
: 'Connection' header is missing

这是我所有代码的 codepen .我已经将其上传到我的托管服务提供商,该提供商正在Node 9.11.2环境中的Apache服务器上通过cPanel使用Passenger.

Here is a codepen with all of my code. I've uploaded it to my hosting provider, which is using Passenger via cPanel on an Apache server in a Node 9.11.2 environment.

从我一直在阅读的内容来看,无法使用AJAX设置连接"标头,这可能会带来安全风险,但由服务器来处理吗?可以手动设置此标头吗?

From what I've been reading, the 'Connection' headers can't be set using AJAX, which is supposedly a security risk, but instead is handled by the server? Is it possible to manually set this header?

通过查看标头,我注意到通常在早期,标头之一总是成功地将协议切换/升级到WebSockets,但是随后平台似乎总是恢复为HTTPS/轮询.在下面我刚才观察到的情况下,实际上在67个实例中,有2个实例成功地切换到WebSockets协议并使用了WebSockets协议.

I have noticed by looking at the headers that early on usually one of them always successfully switches/upgrades the protocol to WebSockets, but then the platform always seems to revert back to HTTPS/polling. In the case below, which I observed just now, there were actually 2 out of now 67 instances whereby it successfully switched to and used the WebSockets protocol.

推荐答案

在启动侦听端口80的HTTP服务器之前,您需要初始化socket.io.在文档中是...

You need to initialize socket.io before you start your HTTP server that is listening on port 80. In the docs it is...

var http = require('http').Server(app);
var io = require('socket.io')(http);

然后再调用http.listen(3000, ...).这将启动套接字.

And then later, you call http.listen(3000, ...). This will start the socket.

在客户端上,您的路径也不正确. Websockets是协议(ws://somedomain.com),就像HTTP.通过http://连接时,这是一个不同的协议.我要么不传递任何东西,要么使用可以在socket.io文档中找到的名称空间.

On the client, your path is also incorrect. Websockets is a protocol (ws://somedomain.com), just like HTTP. When you connect via http:// that is a different protocol. I would either not pass it anything, or use namespaces which you can find in the socket.io documentation.

Socket.io恢复为长时间轮询,有时甚至不使用websocket(这部分是为什么它通常比npm上的ws等库慢)的原因.因此,将http://传递到客户端的io(...)可能与他们的库没有关系,但是我还是要删除它,并按照上面的建议进行操作.

Socket.io reverts to long polling and sometimes doesn't even use websockets (which is partly why it is generally slower than libraries like ws on npm). Because of that, passing http:// into the io(...) on the client may not be wrong with their library, but I would remove it anyway and do my above suggestion.

这篇关于WebSocket握手期间出错:缺少“连接"标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆