Javascript wss 上的 WebSocket 连接失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32693376/
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
WebSocket connection on wss failed
提问by Thabung
I have purchased a certificate and installed in my node.js website.But the https at the browser shows green and is OK.Now, I am trying to establish a socket connection using wss, but it failed. The error at the Javascript client side is like this.
我已经购买了一个证书并安装在我的 node.js 网站上。但是浏览器中的 https 显示为绿色并且可以。现在,我正在尝试使用 wss 建立套接字连接,但失败了。Javascript客户端的错误是这样的。
WebSocket connection to 'wss://securedsitedotcom:3003/call' failed:
WebSocket opening handshake was canceled
Please help!
请帮忙!
Code at client side (Javascript)
客户端代码(Javascript)
var ws = new WebSocket('wss://securedsitedotcom:3003/call');
Code at server side (node.js)
服务器端代码(node.js)
https = require('https');
var server = https.createServer({
key: fs.readFileSync(config.certKeyPath),
cert: fs.readFileSync(config.certCrt),
requestCert: true,
rejectUnauthorized: false
},app);
server.listen(port);
var wss = new ws.Server({
server: server,
path: '/call'
});
Error at the browser console :
浏览器控制台出错:
WebSocket connection to 'wss://securedsitedotcom:3003/call' failed:
WebSocket opening handshake was canceled
回答by w4rt3r
I ran into a similar issue, but I was using a Self Signed Certificate. You mentioned that you bought a Certificate. I guest it is signed by the certificate authority.
我遇到了类似的问题,但我使用的是自签名证书。您提到您购买了证书。我来宾它是由证书颁发机构签署的。
Otherwise, like in my case, non-validated certificate can cause an "opening handshake was cancelled" error. A validated certificate is either validated by a third party (Certificate Authority, ie VeriSign) or explicitly authorized by the client.
否则,就像我的情况一样,未经验证的证书可能会导致“打开握手被取消”错误。经验证的证书要么由第三方(Certificate Authority,即 VeriSign)验证,要么由客户端明确授权。
In my case, VeriSign didn't sign my certificate (self signed), so I had to explicitly authorized it. To do so, I simply need to visit the https URL with the browser (in your case "https://securedsitedotcom:3003/call"). Then a "warning unauthorized host" appear. You need to authorize the exception and than you can make your WSS connection.
就我而言,VeriSign 没有签署我的证书(自签名),所以我必须明确授权它。为此,我只需要使用浏览器访问 https URL(在您的情况下为“ https://securedsitedotcom:3003/call”)。然后出现“警告未经授权的主机”。您需要授权异常,然后才能建立 WSS 连接。
Your server can use any port, it is not bound to 443. 443 is the default port for https but any port can be explicitly specified like you've done.
您的服务器可以使用任何端口,它不绑定到 443。443 是 https 的默认端口,但可以像您所做的那样明确指定任何端口。
I hope it helps someone.
我希望它可以帮助某人。
回答by sjcard
Recent work with Chrome has revealed that if a page is served as https on Chrome, websockets must use wss. And if wss must be used, port 443 must be used (and to boot not any other secure port and so far I have not seen any way to change the port), which may be your problem since your port looks like 3003 above.
最近与 Chrome 的合作表明,如果页面在 Chrome 上作为 https 提供服务,则 websockets 必须使用 wss。如果必须使用 wss,则必须使用端口 443(并且不能引导任何其他安全端口,到目前为止我还没有看到任何更改端口的方法),这可能是您的问题,因为您的端口看起来像上面的 3003。
Right now I am trying to get my IT group to patch/upgrade Apache on that server so mod_proxy_wstunnel can be used to make Apache listening on 443 a reverse proxy and pass all wss traffic through to my websocket server.
现在,我正试图让我的 IT 团队在该服务器上修补/升级 Apache,因此可以使用 mod_proxy_wstunnel 使 Apache 在 443 上侦听反向代理并将所有 wss 流量传递到我的 websocket 服务器。
Hope this helps.
希望这可以帮助。