Skip to content

Instantly share code, notes, and snippets.

@getnamo
Forked from mikeseese/sio-test.js
Created April 8, 2022 07:15
Show Gist options
  • Save getnamo/fe6c9574dc971066813fd291c363ee04 to your computer and use it in GitHub Desktop.
Save getnamo/fe6c9574dc971066813fd291c363ee04 to your computer and use it in GitHub Desktop.
const fs = require("fs");
const http = require("http");
const https = require("https");
const { Server } = require("socket.io");
const express = require("express");
//defaults
const tlsEnabled = true;
const port = 3000;
const app = express();
let server;
if (tlsEnabled) {
const key = fs.readFileSync("key.pem");
const cert = fs.readFileSync("cert.pem");
const options = {
key,
cert,
};
server = https.createServer(options, app);
} else {
server = http.createServer(app);
}
const io = new Server(server);
server.listen(port, () => {
if(tlsEnabled)
{
console.log("SSL Server open on localhost:" + port);
}
else{
console.log("Server open on localhost:" + port);
}
});
io.on("connection", (socket) => {
console.log(socket.id + " connected.");
socket.on("disconnect", (reason) => {
console.log(socket.id + " disconnected: " + reason);
});
});
@getnamo
Copy link
Author

getnamo commented Apr 8, 2022

Forked for minor modifications and log verbosity. Resolve/Reject did not work on my end.

Comment copied from upstream:
Reference documentation: https://nodejs.org/en/knowledge/HTTP/servers/how-to-create-a-HTTPS-server/

To generate a self-signed certificate:

openssl genrsa -out key.pem
openssl req -new -key key.pem -out csr.pem
openssl x509 -req -days 9999 -in csr.pem -signkey key.pem -out cert.pem
rm csr.pem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment