I asked a question before which is in this link and @itgoldman answered that i should use https which was correct but my other problem is that when i wanna access to 443(the specified port for HTTPS) it gives me this error : EACCES permission denied 0.0.0.0:443
Note that 0.0.0.0
is an example ip address first of all here is my code :
const fs = require('fs');
const ws = new require('ws');
const wss = new ws.Server({port : 443,host: "example.com",path: "/hello/"});
const clients = new Set();
var httpsOptions = {
key: fs.readFileSync("ssl key"),
cert: fs.readFileSync("ssl cert")
};
const server = require('https').createServer(httpsOptions, accept);
function accept(req, res) {
if (req.url == '/ws' && req.headers.upgrade &&
req.headers.upgrade.toLowerCase() == 'websocket' &&
// can be Connection: keep-alive, Upgrade
req.headers.connection.match(/\bupgrade\b/i)) {
wss.handleUpgrade(req, req.socket, Buffer.alloc(0), onSocketConnect);
} else if (req.url == '/') { // index.html
fs.createReadStream('./index.html').pipe(res);
} else { // page not found
res.writeHead(404);
res.end();
}
}
server.listen();
function onSocketConnect(ws) {
clients.add(ws);
console.log(`new connection`);
ws.on('message', function(message) {
console.log(`message received: ${message}`);
message = message.slice(0, 50); // max message length will be 50
for(let client of clients) {
client.send(message);
}
});
ws.on('close', function() {
console.log(`connection closed`);
clients.delete(ws);
});
}
I searched on stackoverflow and in this link I saw that this error is for Unix-like systems but it was in cpanel's terminal of my website. I ran the command too but that didn't work. Any help would be appreciated.