1

My code is:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var WebSocket = require('ws')
var ws = new WebSocket('wss://example.com:2217/?key=asdsaasd',{
});

ws.on('open', function() {
    console.log(ws.readyState)
});

ws.on('close', function() {
    console.log(ws.readyState+" closed")
});


ws.on('message', function(message) {
  console.log('received: %s', message);
});
ws.on('error', function(e) {
    console.log(e)
});

When I try to connect, I get the following output:

1

3 closed

I couldn't find any enlightening information on the internet about building WebSockets over SSL/TLS Client using Node JS and I failed. Could you please help me with this?

Sems
  • 11
  • 3
  • Try [this](https://stackoverflow.com/questions/53542307/client-side-websocket-certificate-in-nodejs) – DiniFarb Jun 25 '22 at 17:42
  • While I was searching for an example to code a wss client before, I came across this topic. As far as I understand, the example mentioned in the title you quoted is about coding a wss server, and what I was wondering is coding a client that will connect to such a server. – Sems Jun 25 '22 at 18:10
  • Now, I examined the document on the subject you suggested a little more and I found an example of the client connection, the example is as follows:.https://github.com/websockets/ws/blob/14d9088391ac4495d04e64d76c3b83d4e75f80e2/test/websocket.test.js#L1713 but at this point what i don't understand is how to setup the certificate, i am a bit unfamiliar with this business – Sems Jun 25 '22 at 18:22
  • For Testing you could use a self sign cert created with openssl f. e. Maybe [this](https://devopscube.com/create-self-signed-certificates-openssl/) will help you out – DiniFarb Jun 27 '22 at 06:01

0 Answers0