diff options
author | tomsmeding <tom.smeding@gmail.com> | 2017-03-23 22:34:02 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2017-03-23 22:34:02 +0100 |
commit | 7157893eb0d7cce75a423b42e183ef88bc2130eb (patch) | |
tree | 86feae97013653d3fe14b00e7077186bcba5d7aa /websockets | |
parent | 11b1831b81a89e98fabc771b6a698f11f438b215 (diff) |
Saner websocket API (messages <-> lines)
Diffstat (limited to 'websockets')
-rwxr-xr-x | websockets/server.js | 12 |
1 files changed, 9 insertions, 3 deletions
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"); }); }); |