From 256b7b5bb4deb5828689ca9fa5fccea04043941e Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 28 Aug 2018 16:28:26 +0200 Subject: [PATCH] Phantom viewers potential fix --- www/common/cryptpad-common.js | 10 +++++++++ www/common/outer/async-store.js | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 4652c104f..5b193bd4e 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -1016,7 +1016,17 @@ define([ cb(); }; + /*var onPing = function (data, cb) { + cb(); + }; + + var onTimeout = function () { + //alert("Timeout"); + };*/ + var queries = { + /*PING: onPing, + TIMEOUT: onTimeout,*/ REQUEST_LOGIN: requestLogin, UPDATE_METADATA: common.changeMetadata, UPDATE_TOKEN: function (data) { diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index b53b7cae9..9eb81650c 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -1558,6 +1558,43 @@ define([ rt.proxy.on('reconnect', function (info) { broadcast([], 'NETWORK_RECONNECT', {myId: info.myId}); }); + + /* + // Ping clients regularly to make sure one tab was not closed without sending a removeClient() + // command. This allow us to avoid phantom viewers in pads. + var PING_INTERVAL = 30000; + var MAX_PING = 1000; + var MAX_FAILED_PING = 5; + + setInterval(function () { + var clients = []; + Object.keys(Store.channels).forEach(function (chanId) { + var c = Store.channels[chanId].clients; + Array.prototype.push.apply(clients, c); + }); + clients = Util.deduplicateString(clients); + clients.forEach(function (cId) { + var nb = 0; + var ping = function () { + nb++; + if (nb >= MAX_FAILED_PING) { + Store._removeClient(cId); + postMessage(cId, 'TIMEOUT'); + console.error('TIMEOUT 5 errors'); + return; + } + var to = setTimeout(ping, MAX_PING); + console.log('ping'); + postMessage(cId, 'PING', null, function () { + console.log('pong'); + clearTimeout(to); + }); + }; + ping(); + }); + }, PING_INTERVAL); + */ + }; /**