我正在尝试测试安全的网络套接字,但是遇到了麻烦。这是我的测试:
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”的日志:
{ 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工具时,可以正常连接。
我想念什么吗?请帮忙。
编辑: 我添加ws.on('error')并注销,{ [Error: self signed certificate] code: 'DEPTH_ZERO_SELF_SIGNED_CERT' } 我也尝试遵循此代码,但得到相同的错误。
ws.on('error')
{ [Error: self signed certificate] code: 'DEPTH_ZERO_SELF_SIGNED_CERT' }
该https模块正在拒绝您的自签名证书(正如人们希望的那样)。您可以通过传递一个rejectUnauthorized: false选项(WebSocket将传递给https)来强制其停止检查:
https
rejectUnauthorized: false
WebSocket
var ws = new WebSocket('wss://localhost:15449/', { protocolVersion: 8, origin: 'https://localhost:15449', rejectUnauthorized: false });