Javascript Websocket SSL 连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30902547/
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 SSL connection
提问by cocoa
I am trying to test a secure websocket but I'm having trouble. Here is my test:
我正在尝试测试一个安全的 websocket,但我遇到了麻烦。这是我的测试:
var WebSocket = require('ws');
describe('testing Web Socket', function() {
it('should do stuff', function(done) {
var ws = new WebSocket('wss://localhost:15449/', {
protocolVersion: 8,
origin: 'https://localhost:15449'
});
ws.on('open', function() {
console.log('open!!!');
done();
});
console.log(ws);
});
});
Here's the log of "ws" after it's created:
这是“ws”创建后的日志:
{ domain: null,
_events: { open: [Function] },
_maxListeners: undefined,
_socket: null,
_ultron: null,
_closeReceived: false,
bytesReceived: 0,
readyState: 0,
supports: { binary: true },
extensions: {},
_isServer: false,
url: 'wss://localhost:15449/',
protocolVersion: 8 }
I don't get a log back from open. I am running the project locally and when I use the Chrome Advanced Rest Client tool I am able to connect just fine.
我没有从开放中得到日志。我在本地运行该项目,当我使用 Chrome Advanced Rest Client 工具时,我能够正常连接。
Am I missing something? Please help.
我错过了什么吗?请帮忙。
Edit:I added ws.on('error')
and it logged out { [Error: self signed certificate] code: 'DEPTH_ZERO_SELF_SIGNED_CERT' }
I've also tried following this codebut get the same error.
编辑:我添加ws.on('error')
并注销了{ [Error: self signed certificate] code: 'DEPTH_ZERO_SELF_SIGNED_CERT' }
我也尝试过遵循此代码但得到相同的错误。
回答by Aaron Dufour
The https
module is rejecting your self-signed cert (as one would hope). You can force it to stop checking by passing a rejectUnauthorized: false
option (which WebSocket
will pass down to https
):
该https
模块拒绝您的自签名证书(正如人们所希望的)。您可以通过传递一个rejectUnauthorized: false
选项(WebSocket
将传递给https
)来强制它停止检查:
var ws = new WebSocket('wss://localhost:15449/', {
protocolVersion: 8,
origin: 'https://localhost:15449',
rejectUnauthorized: false
});