Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 760
cocoa Avatar asked Jun 17 '15 21:06

cocoa


People also ask

Does WebSocket use SSL?

The probe supports Secure Sockets Layer (SSL) connections between the probe and WebSocket. SSL connections provide additional security when the probe retrieves alarms from the target systems. To enable SSL connections, obtain any required SSL certificates and Trusted Authority certificates for WebSocket.

How do I connect to HTTPS WebSocket?

You can't use WebSockets over HTTPS, but you can use WebSockets over TLS (HTTPS is HTTP over TLS). Just use "wss://" in the URI.

Does WSS use SSL?

The WebSocket (WS) protocol runs on TCP (like HTTP), and the WSS connection runs on TLS/SSL, which, in turn, runs on TCP. The WebSocket protocol is compatible with HTTP such that the WebSocket connection uses the same ports: the WebSocket default port is 80 and WebSocket Secure (WSS) uses port 443 by default.


1 Answers

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
});
like image 186
Aaron Dufour Avatar answered Nov 11 '22 17:11

Aaron Dufour