diff options
author | tomsmeding <tom.smeding@gmail.com> | 2017-03-28 23:06:16 +0200 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2017-03-28 23:06:16 +0200 |
commit | 9ad0dd977b35d20343babc85a432ff90b95473ad (patch) | |
tree | 31ac901157cc6e46d1da506c0ac60630b63498be | |
parent | 7e06c9d9c64261c12f28fc2ee9181d25ecfb329c (diff) |
websockets: WIP https
-rwxr-xr-x | websockets/server.js | 41 |
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(); }); |