My websocket server, based on node.js, works for ws:// but not for wss://
The server runs on my Raspberry Pi B 3+. Now that I have changed ws:// to wss:// in my JavaScript file, it does not work anymore.
The node.js server:
const WebSocket = require('ws');
var wss = new WebSoket.Server({ port: 4445 });
wss.on('connection', function connection(ws) {
console.log("New client connected.");
ws.on('message', function incoming(data) {
console.log(data);
ws.close();
});
ws.on('close', function close() {
console.log("Client disconnected.");
});
});
The JavaScript client:
var connection = new Websocket('wss://myDomain:4445');
connection.onopen = function () {
connection.send("Hello");
connection.close();
}
connection.onerror = function (error) {
console.log(error);
connection.lose();
}
'myDomain' is a subdomain that refers to the IP of the Raspberry Pi via dns.
I get the following error:
WebSocket connection to 'wss://myDomain:4445/' failed: Error in
connection establishment: net::ERR_CONNECTION_CLOSED
Copyright Notice:Content Author:「Paul」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/56923070/how-to-make-a-server-for-websocket-secure-with-node-js