diff options
-rw-r--r-- | websockets/package-lock.json | 7 | ||||
-rw-r--r-- | websockets/package.json | 2 | ||||
-rwxr-xr-x | websockets/server.js | 98 |
3 files changed, 54 insertions, 53 deletions
diff --git a/websockets/package-lock.json b/websockets/package-lock.json index 8c0a420..bd96131 100644 --- a/websockets/package-lock.json +++ b/websockets/package-lock.json @@ -2,10 +2,11 @@ "name": "tomsg-webserver", "version": "0.1.0", "lockfileVersion": 1, + "requires": true, "dependencies": { - "uws": { - "version": "https://registry.npmjs.org/uws/-/uws-0.14.5.tgz", - "integrity": "sha1-Z6rzPEaypYel9mZtAPdpEyjxSdw=" + "uWebSockets.js": { + "version": "github:uNetworking/uWebSockets.js#aca45709610cf42392f944230b38ff1f32c4c378", + "from": "github:uNetworking/uWebSockets.js#v18.1.0" } } } diff --git a/websockets/package.json b/websockets/package.json index aaefc96..a92d034 100644 --- a/websockets/package.json +++ b/websockets/package.json @@ -17,6 +17,6 @@ "author": "Tom Smeding <tom.smeding@gmail.com> (https://tomsmeding.com)", "license": "MIT", "dependencies": { - "uws": "^0.14" + "uWebSockets.js": "github:uNetworking/uWebSockets.js#v18.1.0" } } diff --git a/websockets/server.js b/websockets/server.js index 2184793..86307e6 100755 --- a/websockets/server.js +++ b/websockets/server.js @@ -2,7 +2,7 @@ const fs=require("fs"); const net=require("net"); const https=require("https"); -const WebSocket=require("uws"); +const WebSocket=require("uWebSockets.js"); const PORT=29546; @@ -29,60 +29,60 @@ if(process.getuid()==0){ } -let httpsServer,wsServer; +let 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`); + wsServer=WebSocket.SSLApp({ + key_file_name: httpsConfig.key, + cert_file_name: httpsConfig.cert, }); } else { - wsServer=new WebSocket.Server({port:PORT},()=>{ - console.log(`Listening for websocket http requests on port ${PORT}`); - }); + wsServer=WebSocket.App(); } -wsServer.on("connection",(sock)=>{ - let netconn=null; - let buffer=[]; - let linebuf=""; - netconn=net.connect(upstream.port,upstream.host,()=>{ - for(const item of buffer){ - netconn.write(item+"\n"); - } - buffer=[]; - }); - netconn.on("close",()=>{ - sock.close(); - }); - netconn.on("data",(data)=>{ - linebuf+=data; - let idx; - while((idx=linebuf.indexOf("\n"))!=-1){ - sock.send(linebuf.slice(0,idx)); - linebuf=linebuf.slice(idx+1); - } - }); +wsServer=wsServer.ws("/*",{ + open: sock=>{ + const stateobj={netconn: null, buffer: [], sock_closed: false}; + sock["tomsgdata"]=stateobj; + let linebuf=""; - sock.on("close",()=>{ - netconn.end(); - }); - sock.on("message",(data)=>{ - if(netconn.connecting)buffer.push(data); - else netconn.write(data+"\n"); - }); + stateobj.netconn=net.connect(upstream.port,upstream.host,()=>{ + for(const item of stateobj.buffer){ + stateobj.netconn.write(item); + stateobj.netconn.write("\n"); + } + stateobj.buffer=[]; + }); + stateobj.netconn.on("close",()=>{ + if(!stateobj.sock_closed)sock.close(); + }); + stateobj.netconn.on("data",(data)=>{ + linebuf+=data; + let idx; + while((idx=linebuf.indexOf("\n"))!=-1){ + sock.send(linebuf.slice(0,idx)); + linebuf=linebuf.slice(idx+1); + } + }); + }, + message: (sock,data,isBinary)=>{ + const stateobj=sock["tomsgdata"]; + data=new Uint8Array(data); + if(stateobj.netconn.connecting){ + stateobj.buffer.push(data); + } else { + stateobj.netconn.write(data); + stateobj.netconn.write("\n"); + } + }, + close: sock=>{ + const stateobj=sock["tomsgdata"]; + stateobj.sock_closed=true; + stateobj.netconn.end(); + } }); -if(httpsConfig){ - httpsServer.listen(PORT,()=>{ - console.log(`HTTPS server bound on port ${PORT}`); - }); -} - -process.on("SIGINT",()=>{ - console.log("Closing server..."); - wsServer.close(); - if(httpsConfig)httpsServer.close(); +wsServer=wsServer.listen(PORT,listenSocket=>{ + if(listenSocket){ + console.log(`Websocket server bound on port ${PORT}`); + } }); |