From 13deef189d2ed69e3afa0de50d046cba443bf3ed Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Thu, 24 Apr 2014 23:42:44 +0100 Subject: [PATCH] Add ws heartbeat to keep connection alive through firewall --- nodes/core/core/58-debug.html | 3 +++ nodes/core/core/58-debug.js | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/nodes/core/core/58-debug.html b/nodes/core/core/58-debug.html index ce8307b16..71aae718d 100644 --- a/nodes/core/core/58-debug.html +++ b/nodes/core/core/58-debug.html @@ -134,6 +134,9 @@ } ws.onmessage = function(event) { var o = JSON.parse(event.data); + if (o.heartbeat) { + return; + } //console.log(msg); var msg = document.createElement("div"); msg.onmouseover = function() { diff --git a/nodes/core/core/58-debug.js b/nodes/core/core/58-debug.js index 1c77a047c..a88c6a026 100644 --- a/nodes/core/core/58-debug.js +++ b/nodes/core/core/58-debug.js @@ -57,6 +57,26 @@ function DebugNode(n) { }); } +var lastSentTime = (new Date()).getTime(); + +setInterval(function() { + var now = (new Date()).getTime(); + if (now-lastSentTime > 15000) { + lastSentTime = now; + for (var i in DebugNode.activeConnections) { + var ws = DebugNode.activeConnections[i]; + try { + var p = JSON.stringify({heartbeat:lastSentTime}); + ws.send(p); + } catch(err) { + util.log("[debug] ws heartbeat error : "+err); + } + } + } +}, 15000); + + + RED.nodes.registerType("debug",DebugNode); DebugNode.send = function(msg) { @@ -85,7 +105,7 @@ DebugNode.send = function(msg) { if (msg.msg.length > debuglength) { msg.msg = msg.msg.substr(0,debuglength) +" ...."; } - + for (var i in DebugNode.activeConnections) { var ws = DebugNode.activeConnections[i]; try { @@ -95,6 +115,7 @@ DebugNode.send = function(msg) { util.log("[debug] ws error : "+err); } } + lastSentTime = (new Date()).getTime(); } DebugNode.activeConnections = [];