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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:54:59  来源:igfitidea点击:

Websocket SSL connection

javascriptnode.jssslwebsocket

提问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 httpsmodule is rejecting your self-signed cert (as one would hope). You can force it to stop checking by passing a rejectUnauthorized: falseoption (which WebSocketwill pass down to https):

https模块拒绝您的自签名证书(正如人们所希望的)。您可以通过传递一个rejectUnauthorized: false选项(WebSocket将传递给https)来强制它停止检查:

var ws = new WebSocket('wss://localhost:15449/', {
  protocolVersion: 8,
  origin: 'https://localhost:15449',
  rejectUnauthorized: false
});