aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xwebsockets/server.js41
1 files changed, 35 insertions, 6 deletions
diff --git a/websockets/server.js b/websockets/server.js
index d0b963b..67ed6d1 100755
--- a/websockets/server.js
+++ b/websockets/server.js
@@ -1,5 +1,7 @@
#!/usr/bin/env node
+const fs=require("fs");
const net=require("net");
+const https=require("https");
const WebSocket=require("uws");
const PORT=29546;
@@ -10,10 +12,30 @@ const upstream={
};
-const server=new WebSocket.Server({port:PORT},()=>{
- console.log(`Listening for websocket http requests on port ${PORT}`);
-});
-server.on("connection",(sock)=>{
+let httpsConfig=null;
+if(process.argv.length>=4){
+ httpsConfig={
+ key: fs.readFileSync(process.argv[2]),
+ cert: fs.readFileSync(process.argv[3]),
+ };
+}
+
+
+let httpsServer,wsServer;
+if(httpsConfig){
+ httpsServer=https.createServer(httpsConfig,(req,res)=>{
+ console.log("Got request in httpsServer?");
+ // req.socket.end(); // ?
+ });
+ wsServer=new WebSocket.Server({server:httpsServer},()=>{
+ console.log(`Bound websocket server to https server`);
+ });
+} else {
+ wsServer=new WebSocket.Server({port:PORT},()=>{
+ console.log(`Listening for websocket http requests on port ${PORT}`);
+ });
+}
+wsServer.on("connection",(sock)=>{
let netconn=null;
let buffer=[];
let linebuf="";
@@ -45,7 +67,14 @@ server.on("connection",(sock)=>{
});
});
+if(httpsConfig){
+ httpsServer.listen(PORT,()=>{
+ console.log(`HTTPS server bound on port ${PORT}`);
+ });
+}
+
process.on("SIGINT",()=>{
- console.log("Closing websocket server...");
- server.close();
+ console.log("Closing server...");
+ wsServer.close();
+ if(httpsConfig)httpsServer.close();
});