diff --git a/meshuser.js b/meshuser.js index b7eb4b73..ebed13ba 100644 --- a/meshuser.js +++ b/meshuser.js @@ -75,11 +75,23 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use // Clean a IPv6 address that encodes a IPv4 address function cleanRemoteAddr(addr) { if (addr.startsWith('::ffff:')) { return addr.substring(7); } else { return addr; } } + // Send a PING/PONG message + function sendPing() { obj.ws.send('{"action":"ping"}'); } + function sendPong() { obj.ws.send('{"action":"pong"}'); } + + // Setup the agent PING/PONG timers + if ((typeof args.browserping == 'number') && (obj.pingtimer == null)) { obj.pingtimer = setInterval(sendPing, args.browserping * 1000); } + else if ((typeof args.browserpong == 'number') && (obj.pongtimer == null)) { obj.pongtimer = setInterval(sendPong, args.browserpong * 1000); } + // Disconnect this user obj.close = function (arg) { if ((arg == 1) || (arg == null)) { try { ws.close(); parent.parent.debug('user', 'Soft disconnect'); } catch (e) { console.log(e); } } // Soft close, close the websocket if (arg == 2) { try { ws._socket._parent.end(); parent.parent.debug('user', 'Hard disconnect'); } catch (e) { console.log(e); } } // Hard close, close the TCP socket + // Perform timer cleanup + if (obj.pingtimer) { clearInterval(obj.pingtimer); delete obj.pingtimer; } + if (obj.pongtimer) { clearInterval(obj.pongtimer); delete obj.pongtimer; } + // Perform cleanup parent.parent.RemoveAllEventDispatch(ws); if (obj.serverStatsTimer != null) { clearInterval(obj.serverStatsTimer); delete obj.serverStatsTimer; } @@ -418,6 +430,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use if (common.validateString(command.action, 3, 32) == false) return; // Action must be a string between 3 and 32 chars switch (command.action) { + case 'pong': { break; } // NOP case 'ping': { try { ws.send(JSON.stringify({ action: 'pong' })); } catch (ex) { } break; } case 'intersession': { diff --git a/public/scripts/meshcentral.js b/public/scripts/meshcentral.js index a09ca2ce..260723a0 100644 --- a/public/scripts/meshcentral.js +++ b/public/scripts/meshcentral.js @@ -46,6 +46,7 @@ var MeshServerCreateControl = function (domain, authCookie) { var message; try { message = JSON.parse(e.data); } catch (e) { return; } if ((typeof message != 'object') || (message.action == 'pong')) { return; } + if (message.action == 'ping') { obj.send({ action: 'pong' }); } if (message.action == 'close') { if (message.msg) { console.log(message.msg); } obj.Stop(message.cause); return; } if (obj.trace) { console.log('RECV', message); } if (obj.onMessage) obj.onMessage(obj, message); diff --git a/sample-config.json b/sample-config.json index 29064190..9273bef2 100644 --- a/sample-config.json +++ b/sample-config.json @@ -35,6 +35,8 @@ "_Nice404": false, "_ClickOnce": false, "_SelfUpdate": true, + "_BrowserPing": 60, + "_BrowserPong": 60, "_AgentPing": 60, "_AgentPong": 60, "_AgentIdleTimeout": 150,