I have updated my website by making the domain name point to cloudflare servers, then, have cloudflare point to my webhost. Before changing the website from http to https and from us ws to wss, everything was working fine, but after, it kept of saying "WebSocket connection to "wss://server" failed".
Client Side:
ws = new WebSocket('wss://ec2-3-97-91-10.ca-central-1.compute.amazonaws.com:443');
Server Side:
const express = require('express');
const https = require('https');
const WebSocket = require('ws');
const fs = require('fs');
var privateKey = fs.readFileSync('SSL PRIVATE KEY.key', 'utf8');
var certificate = fs.readFileSync('SSL CERTIFICATE.crt', 'utf8');
var credentials = {key: privateKey, cert: certificate};
const port = 443;
var Application = express();
const server = https.createServer(credentials, Application);
var wss = new WebSocket.Server({ server })
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(data) {
wss.clients.forEach(function each(client) {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(data.toString());
}
})
})
})
server.listen(port, function() {
console.log(`Server is listening on ${port}!`)
})
The SSL certificate and private key are gotten from cloudflare.
If you want the full code, I uploaded it to a github repo here: https://github.com/stampixel/stampixel.ga
The domain im using is https://stampixel.ga