Added browser ping/pong settings.

Ylian Saint-Hilaire committed May 6, 2020 at 19:23 UTC b96ababf0bfa76fc3de1f4a81fe39ea6820c7bbf
3 files changed +16
meshuser.js
+13
@@ -75,11 +75,23 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
75 // Clean a IPv6 address that encodes a IPv4 address
76 function cleanRemoteAddr(addr) { if (addr.startsWith('::ffff:')) { return addr.substring(7); } else { return addr; } }
77
78 + // Send a PING/PONG message
79 + function sendPing() { obj.ws.send('{"action":"ping"}'); }
80 + function sendPong() { obj.ws.send('{"action":"pong"}'); }
81 +
82 + // Setup the agent PING/PONG timers
83 + if ((typeof args.browserping == 'number') && (obj.pingtimer == null)) { obj.pingtimer = setInterval(sendPing, args.browserping * 1000); }
84 + else if ((typeof args.browserpong == 'number') && (obj.pongtimer == null)) { obj.pongtimer = setInterval(sendPong, args.browserpong * 1000); }
85 +
86 // Disconnect this user
87 obj.close = function (arg) {
88 if ((arg == 1) || (arg == null)) { try { ws.close(); parent.parent.debug('user', 'Soft disconnect'); } catch (e) { console.log(e); } } // Soft close, close the websocket
89 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
90
91 + // Perform timer cleanup
92 + if (obj.pingtimer) { clearInterval(obj.pingtimer); delete obj.pingtimer; }
93 + if (obj.pongtimer) { clearInterval(obj.pongtimer); delete obj.pongtimer; }
94 +
95 // Perform cleanup
96 parent.parent.RemoveAllEventDispatch(ws);
97 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
430 if (common.validateString(command.action, 3, 32) == false) return; // Action must be a string between 3 and 32 chars
431
432 switch (command.action) {
433 + case 'pong': { break; } // NOP
434 case 'ping': { try { ws.send(JSON.stringify({ action: 'pong' })); } catch (ex) { } break; }
435 case 'intersession':
436 {
public/scripts/meshcentral.js
+1
@@ -46,6 +46,7 @@ var MeshServerCreateControl = function (domain, authCookie) {
46 var message;
47 try { message = JSON.parse(e.data); } catch (e) { return; }
48 if ((typeof message != 'object') || (message.action == 'pong')) { return; }
49 + if (message.action == 'ping') { obj.send({ action: 'pong' }); }
50 if (message.action == 'close') { if (message.msg) { console.log(message.msg); } obj.Stop(message.cause); return; }
51 if (obj.trace) { console.log('RECV', message); }
52 if (obj.onMessage) obj.onMessage(obj, message);
sample-config.json
+2
@@ -35,6 +35,8 @@
35 "_Nice404": false,
36 "_ClickOnce": false,
37 "_SelfUpdate": true,
38 + "_BrowserPing": 60,
39 + "_BrowserPong": 60,
40 "_AgentPing": 60,
41 "_AgentPong": 60,
42 "_AgentIdleTimeout": 150,