Chrome WebSocket 连接立即关闭
我一直在尝试使用nodejs设置wss服务器,并且在尝试使用chrome连接到它时遇到了问题。当所有扩展被禁用并且在隐身窗口中时,问题仍然出现,所以我已经排除了这个问题。
当尝试使用 chrome 连接时,出现错误:
WebSocket connection to 'wss://www.domain-name.com/' failed:
没有给出原因。在服务器上,立即调用 socket.on('close') 并描述“远程对等点断开连接” close 事件的 wasClean = false。从 safari 和 Firefox 连接时不会发生此错误,因此我不确定在哪里查看导致该错误的原因。它在 AWS Lightsail 上运行,并通过 Apache 代理服务器运行。
客户端代码:
var socket = new WebSocket("wss://www.domain-name.com", 'JSON')
socket.onopen = function (event) {
console.log('open');
socket.send('socket opened')};
socket.onclose = function (event) {
console.log(event)};
socket.onmessage = function(message) {
console.log('receiving message from server...')};
和服务器代码:
const WebSocketServer = require('websocket').server;
app = express()
var server = app.listen(3000, () => {
console.log('Server started');
});
app.use(express.static('public'));
var wsServer = new WebSocketServer({
httpServer: server
});
wsServer.on('request', function(request){
console.log('New connection');
var connection = request.accept(null, request.origin);
connection.send('welcome from server...');
connection.on('message', function(message){
console.log(message)};
connection.on('close', function(reasonCode, description) {
console.log('disconnecting', reasonCode, description);
});
});
在切换到安全的 WebSocket 服务器之前,我也遇到了相同的错误。任何帮助将不胜感激,我已经没有地方可以查找,也没有办法尝试获取更多信息来帮助解决问题。
编辑:它似乎可以在我手机上的 chrome 上运行,但不能在我朋友手机上的 chrome 上运行?
I have been trying to setup a wss server using nodejs, and have encountered a problem when trying to connect to it using chrome. The problem still occurs with all extensions disabled and in an incognito window so I've ruled that out as the problem.
When trying to connect using chrome, I get the error:
WebSocket connection to 'wss://www.domain-name.com/' failed:
with no reason given. On the server, socket.on('close') is called immediately with description "Connection dropped by remote peer" The close event has wasClean = false. This error does not occur when connecting from safari and Firefox so I'm not really sure where to look to see what's causing it. It's running on AWS Lightsail, and through an Apache proxy server.
The client code:
var socket = new WebSocket("wss://www.domain-name.com", 'JSON')
socket.onopen = function (event) {
console.log('open');
socket.send('socket opened')};
socket.onclose = function (event) {
console.log(event)};
socket.onmessage = function(message) {
console.log('receiving message from server...')};
And the server code:
const WebSocketServer = require('websocket').server;
app = express()
var server = app.listen(3000, () => {
console.log('Server started');
});
app.use(express.static('public'));
var wsServer = new WebSocketServer({
httpServer: server
});
wsServer.on('request', function(request){
console.log('New connection');
var connection = request.accept(null, request.origin);
connection.send('welcome from server...');
connection.on('message', function(message){
console.log(message)};
connection.on('close', function(reasonCode, description) {
console.log('disconnecting', reasonCode, description);
});
});
I also got the same error before switching to a secure WebSocket server. Any help would be appreciated, I've run out of places to look and ways to try and get more information to help out what the problem is.
EDIT: it seems to work on chrome on my phone, but not on chrome on my friends phone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于接受连接时未指定协议。经过大约 20 小时处理同一错误并实现 SSL 证书以使其正常工作后,我将:更改
为:
出于某种原因,chrome 给出了一条非常无用的错误消息。 Microsoft Edge 也会发生同样的错误,但给出了更有用的错误消息,以便我可以弄清楚发生了什么。
The problem was not specifying the protocol when accepting the connection. After about 20 hours working on the same bug and implementing an SSL certificate to get it to work, I changed:
to:
For some reason the chrome gives a really unhelpful error message. Microsoft edge the same error occurs, but gives a much more helpful error message so I could work out what was going on.
就我而言,这是由于将未使用的
options
值作为第三个参数传递给 WebSocket 构造函数造成的。 Node.js 的 ws 模块支持 options 参数,但浏览器不支持;然而,Chrome 没有显示干净的错误消息,而是在没有详细说明的情况下关闭了连接。In my case, this was caused by passing an unused
options
value as the third parameter to the WebSocket constructor. The options parameter is supported by Node.js's ws module but not by browsers; however, instead of displaying a clean error message, Chrome closed the connection without a good description.