From dd7bb28b6af33a1257fa48096847c101a3096d5b Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Sun, 28 Jan 2018 10:44:02 +0000 Subject: [PATCH] Fix debug comms batching with multiple connections --- red/api/editor/comms.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/red/api/editor/comms.js b/red/api/editor/comms.js index 4da819489..d0e0cab2c 100644 --- a/red/api/editor/comms.js +++ b/red/api/editor/comms.js @@ -70,6 +70,9 @@ function start() { wsServer.on('connection',function(ws) { log.audit({event: "comms.open"}); var pendingAuth = (settings.adminAuth != null); + ws._nr_stack = []; + ws._nr_ok2tx = true; + if (!pendingAuth) { activeConnections.push(ws); } else { @@ -190,23 +193,22 @@ function publish(topic,data,retain) { } } -var stack = []; -var ok2tx = true; function publishTo(ws,topic,data) { - if (topic && data) { stack.push({topic:topic,data:data}); } - - if (ok2tx && (stack.length > 0)) { - ok2tx = false; + if (topic && data) { + ws._nr_stack.push({topic:topic,data:data}); + } + if (ws._nr_ok2tx && (ws._nr_stack.length > 0)) { + ws._nr_ok2tx = false; try { - ws.send(JSON.stringify(stack)); + ws.send(JSON.stringify(ws._nr_stack)); } catch(err) { removeActiveConnection(ws); removePendingConnection(ws); log.warn(log._("comms.error-send",{message:err.toString()})); } - stack = []; - var txtout = setTimeout(function() { - ok2tx = true; + ws._nr_stack = []; + setTimeout(function() { + ws._nr_ok2tx = true; publishTo(ws); }, 50); // TODO: OK so a 50mS update rate should prob not be hard-coded }