From 7157893eb0d7cce75a423b42e183ef88bc2130eb Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Thu, 23 Mar 2017 22:34:02 +0100 Subject: Saner websocket API (messages <-> lines) --- websockets/server.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'websockets') diff --git a/websockets/server.js b/websockets/server.js index 6b32cba..d0b963b 100755 --- a/websockets/server.js +++ b/websockets/server.js @@ -16,10 +16,11 @@ const server=new WebSocket.Server({port:PORT},()=>{ server.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); + netconn.write(item+"\n"); } buffer=[]; }); @@ -27,7 +28,12 @@ server.on("connection",(sock)=>{ sock.close(); }); netconn.on("data",(data)=>{ - sock.send(data); + linebuf+=data; + let idx; + while((idx=linebuf.indexOf("\n"))!=-1){ + sock.send(linebuf.slice(0,idx)); + linebuf=linebuf.slice(idx+1); + } }); sock.on("close",()=>{ @@ -35,7 +41,7 @@ server.on("connection",(sock)=>{ }); sock.on("message",(data)=>{ if(netconn.connecting)buffer.push(data); - else netconn.write(data); + else netconn.write(data+"\n"); }); }); -- cgit v1.2.3-70-g09d2