Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

javascript - Websocket SSL connection

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);
  });
});

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 }

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.

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 code but get the same error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...