Websocket SSL连接 [英] Websocket SSL connection
问题描述
我正在尝试测试一个安全的websocket,但我遇到了麻烦。这是我的测试:
I am trying to test a secure websocket but I'm having trouble. Here is my test:
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);
});
});
这是创建后ws的日志:
Here's the log of "ws" after it's created:
{ 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 }
我没有从开放状态回来。我在本地运行该项目,当我使用Chrome Advanced Rest Client工具时,我能够正常连接。
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.
我错过了什么吗?请帮忙。
Am I missing something? Please help.
编辑:
我添加了 ws.on('error')
并注销 {[错误:自签名证书]代码:'DEPTH_ZERO_SELF_SIGNED_CERT'}
我也试过这个代码但得到同样的错误。
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 code but get the same error.
推荐答案
https
模块拒绝您的自签名证书(如人们希望)。您可以通过传递 rejectUnauthorized:false
选项强制它停止检查( WebSocket
将传递给 https
):
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
):
var ws = new WebSocket('wss://localhost:15449/', {
protocolVersion: 8,
origin: 'https://localhost:15449',
rejectUnauthorized: false
});
这篇关于Websocket SSL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!