简体   繁体   English

Chrome WebSocket 连接立即关闭

[英]Chrome WebSocket connection closes immediately

I have been trying to setup a wss server using nodejs, and have encountered a problem when trying to connect to it using chrome.我一直在尝试使用 nodejs 设置 wss 服务器,并且在尝试使用 chrome 连接到它时遇到了问题。 The problem still occurs with all extensions disabled and in an incognito window so I've ruled that out as the problem.禁用所有扩展并在隐身 window 中仍然会出现此问题,因此我已将其排除为问题。

When trying to connect using chrome, I get the error:尝试使用 chrome 连接时,出现错误:

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.在服务器上,socket.on('close') 被立即调用,描述为“连接被远程对等点断开”关闭事件有 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.从 safari 和 Firefox 连接时不会发生此错误,所以我不确定在哪里查看是什么原因造成的。 It's running on AWS Lightsail, and through an Apache proxy server.它在 AWS Lightsail 上运行,并通过 Apache 代理服务器。

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.在切换到安全的 WebSocket 服务器之前,我也遇到了同样的错误。 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?编辑:它似乎适用于我手机上的 chrome,但不适用于我朋友手机上的 chrome?

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:在处理相同的错误并实施 SSL 证书以使其工作大约 20 小时后,我更改了:

request.accept(null, request.origin);

to:至:

request.accept('json', request.origin);

For some reason the chrome gives a really unhelpful error message.出于某种原因,chrome 给出了一个非常无用的错误消息。 Microsoft edge the same error occurs, but gives a much more helpful error message so I could work out what was going on. Microsoft edge 发生了相同的错误,但提供了更有用的错误消息,因此我可以弄清楚发生了什么。

In my case, this was caused by passing an unused options value as the third parameter to the WebSocket constructor.在我的情况下,这是由于将未使用的options值作为第三个参数传递给 WebSocket 构造函数引起的。 The options parameter is supported by Node.js's ws module but not by browsers ; options 参数由 Node.js 的 ws 模块支持,但浏览器不支持; however, instead of displaying a clean error message, Chrome closed the connection without a good description.但是,Chrome 并没有显示干净的错误消息,而是在没有很好描述的情况下关闭了连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM