-
-
Save getnamo/fe6c9574dc971066813fd291c363ee04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: