1

I am not able to connect to wss on localhost. I have the following setup:

I created certs using

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365

I started a wss server

const server = https.createServer({
  cert: fs.readFileSync('./cert.pem'),
  key: fs.readFileSync('./key.pem'),
  passphrase: 'ledtesting'
});

const wss = new WebSocket.Server({ server });

server.listen(8000, 'localhost',function listening () {
  console.log(server.address().port);
  const ws = new WebSocket(`wss://localhost:${server.address().port}`, {
    rejectUnauthorized: false
  });
});

I am running an electron app and have a webpage to connect to this web socket server using:

var localsockUrl = "wss://localhost:8000/";
var localsocket = new WebSocket(localsockUrl);

I get the following error:

connection to 'wss://localhost:8000/' failed: WebSocket opening handshake was canceled

How can I access the wss on localhost on an electron app?

Pavan K
  • 4,085
  • 8
  • 41
  • 72
  • Does it work when you try a non-encrypted connection? Plain http server and then `ws://locahost:8000` URL? If so, then you can conclude that you probably have an https setup problem (either untrusted certificates or some sort of auth problem). If not, then the problem will be simpler to debug without an encrypted connection and you can look at network traces in the Chrome debugger to see what you might learn from that. – jfriend00 Sep 04 '17 at 23:36
  • I can connect it with ws:// with wss:// since I am using a self signed certificate I am having this issue. Any way to accept the self signed cert? – Pavan K Sep 05 '17 at 07:28
  • I used wscat to debug and this is the error I get : error: Error: self signed certificate – Pavan K Sep 05 '17 at 07:28
  • You have to teach your browser to accept your certificate. By default, it won't accept self-signed certificates. See [Why are self signed certificates not trusted and is there a way to make them trusted?](https://security.stackexchange.com/questions/112768/why-are-self-signed-certificates-not-trusted-and-is-there-a-way-to-make-them-tru). – jfriend00 Sep 05 '17 at 07:29
  • I understand that, thank you. However, I am serving the app using electron and I can't let it accept my certs or rather I do not know how to? any idea or pointers? – Pavan K Sep 05 '17 at 07:37
  • I don't understand. Is the electron app the client or the server? – jfriend00 Sep 05 '17 at 07:49
  • Perhaps this: [How do i trust a self signed certificate from an electron app?](https://stackoverflow.com/questions/38986692/how-do-i-trust-a-self-signed-certificate-from-an-electron-app) will help? More articles available in a Google search. – jfriend00 Sep 05 '17 at 07:50

1 Answers1

2

In the electron app setup, You can ignore certificate errors by:

app.commandLine.appendSwitch("ignore-certificate-errors");
Pavan K
  • 4,085
  • 8
  • 41
  • 72