mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-20 23:46:14 -04:00
[feature] Only automatically reconnect if can establish connection to server
Avoid trying to reload pad when network is not available.
This commit is contained in:
parent
0eae83f252
commit
384697f653
3 changed files with 60 additions and 19 deletions
|
@ -74,7 +74,7 @@
|
||||||
"pad.modals.connected": "Connected.",
|
"pad.modals.connected": "Connected.",
|
||||||
"pad.modals.reconnecting": "Reconnecting to your pad..",
|
"pad.modals.reconnecting": "Reconnecting to your pad..",
|
||||||
"pad.modals.forcereconnect": "Force reconnect",
|
"pad.modals.forcereconnect": "Force reconnect",
|
||||||
"pad.modals.reconnecttimer": "This pad will be automatically reconnected in ",
|
"pad.modals.reconnecttimer": "Trying to reconnect in ",
|
||||||
"pad.modals.cancel": "Cancel",
|
"pad.modals.cancel": "Cancel",
|
||||||
|
|
||||||
"pad.modals.userdup": "Opened in another window",
|
"pad.modals.userdup": "Opened in another window",
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
|
|
||||||
exports.showCountDownTimerToReconnectOnModal = function($modal) {
|
exports.showCountDownTimerToReconnectOnModal = function($modal, pad) {
|
||||||
if (clientVars.automaticReconnectionTimeout && $modal.is('.with_reconnect_timer')) {
|
if (clientVars.automaticReconnectionTimeout && $modal.is('.with_reconnect_timer')) {
|
||||||
createCountDownElementsIfNecessary($modal);
|
createCountDownElementsIfNecessary($modal);
|
||||||
|
|
||||||
var timer = createTimerForModal($modal);
|
var timer = createTimerForModal($modal, pad);
|
||||||
|
|
||||||
$modal.find('#cancelreconnect').one('click', function() {
|
$modal.find('#cancelreconnect').one('click', function() {
|
||||||
timer.cancel();
|
timer.cancel();
|
||||||
|
@ -22,7 +22,7 @@ var createCountDownElementsIfNecessary = function($modal) {
|
||||||
|
|
||||||
// create extra DOM elements, if they don't exist
|
// create extra DOM elements, if they don't exist
|
||||||
var $reconnectTimerMessage = $('<p class="reconnecttimer"> \
|
var $reconnectTimerMessage = $('<p class="reconnecttimer"> \
|
||||||
<span data-l10n-id="pad.modals.reconnecttimer">This pad will be automatically reconnected in </span> \
|
<span data-l10n-id="pad.modals.reconnecttimer">Trying to reconnect in </span> \
|
||||||
<span class="timetoexpire"></span> \
|
<span class="timetoexpire"></span> \
|
||||||
</p>');
|
</p>');
|
||||||
var $cancelReconnect = $('<button id="cancelreconnect" data-l10n-id="pad.modals.cancel">Cancel</button>');
|
var $cancelReconnect = $('<button id="cancelreconnect" data-l10n-id="pad.modals.cancel">Cancel</button>');
|
||||||
|
@ -39,13 +39,20 @@ var localize = function($element) {
|
||||||
html10n.translateElement(html10n.translations, $element.get(0));
|
html10n.translateElement(html10n.translations, $element.get(0));
|
||||||
};
|
};
|
||||||
|
|
||||||
var createTimerForModal = function($modal) {
|
var createTimerForModal = function($modal, pad) {
|
||||||
var timer = new CountDownTimer(clientVars.automaticReconnectionTimeout);
|
var timeUntilReconnection = clientVars.automaticReconnectionTimeout * reconnectionTries.nextTry();
|
||||||
|
var timer = new CountDownTimer(timeUntilReconnection);
|
||||||
|
|
||||||
timer.onTick(function(minutes, seconds) {
|
timer.onTick(function(minutes, seconds) {
|
||||||
updateCountDownTimerMessage($modal, minutes, seconds);
|
updateCountDownTimerMessage($modal, minutes, seconds);
|
||||||
}).onExpire(function() {
|
}).onExpire(function() {
|
||||||
reconnect($modal);
|
var wasANetworkError = $modal.is('.disconnected');
|
||||||
|
if (wasANetworkError) {
|
||||||
|
// cannot simply reconnect, client is having issues to establish connection to server
|
||||||
|
waitUntilClientCanConnectToServerAndThen(function() { forceReconnection($modal); }, pad);
|
||||||
|
} else {
|
||||||
|
forceReconnection($modal);
|
||||||
|
}
|
||||||
}).start();
|
}).start();
|
||||||
|
|
||||||
return timer;
|
return timer;
|
||||||
|
@ -62,7 +69,20 @@ var toggleAutomaticReconnectionOption = function($modal, disableAutomaticReconne
|
||||||
$modal.find('#defaulttext').toggleClass('hidden', !disableAutomaticReconnect);
|
$modal.find('#defaulttext').toggleClass('hidden', !disableAutomaticReconnect);
|
||||||
}
|
}
|
||||||
|
|
||||||
var reconnect = function($modal) {
|
var waitUntilClientCanConnectToServerAndThen = function(callback, pad) {
|
||||||
|
whenConnectionIsRestablishedWithServer(callback, pad);
|
||||||
|
pad.socket.connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
var whenConnectionIsRestablishedWithServer = function(callback, pad) {
|
||||||
|
// only add listener for the first try, don't need to add another listener
|
||||||
|
// on every unsuccessful try
|
||||||
|
if (reconnectionTries.counter === 1) {
|
||||||
|
pad.socket.once('connect', callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var forceReconnection = function($modal) {
|
||||||
$modal.find('#forcereconnect').click();
|
$modal.find('#forcereconnect').click();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,6 +93,20 @@ var updateCountDownTimerMessage = function($modal, minutes, seconds) {
|
||||||
$modal.find('.timetoexpire').text(minutes + ':' + seconds);
|
$modal.find('.timetoexpire').text(minutes + ':' + seconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// store number of tries to reconnect to server, in order to increase time to wait
|
||||||
|
// until next try
|
||||||
|
var reconnectionTries = {
|
||||||
|
counter: 0,
|
||||||
|
|
||||||
|
nextTry: function() {
|
||||||
|
// double the time to try to reconnect on every time reconnection fails
|
||||||
|
var nextCounterFactor = Math.pow(2, this.counter);
|
||||||
|
this.counter++;
|
||||||
|
|
||||||
|
return nextCounterFactor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Timer based on http://stackoverflow.com/a/20618517.
|
// Timer based on http://stackoverflow.com/a/20618517.
|
||||||
// duration: how many **seconds** until the timer ends
|
// duration: how many **seconds** until the timer ends
|
||||||
// granularity (optional): how many **milliseconds** between each 'tick' of timer. Default: 1000ms (1s)
|
// granularity (optional): how many **milliseconds** between each 'tick' of timer. Default: 1000ms (1s)
|
||||||
|
@ -92,28 +126,34 @@ CountDownTimer.prototype.start = function() {
|
||||||
this.running = true;
|
this.running = true;
|
||||||
var start = Date.now(),
|
var start = Date.now(),
|
||||||
that = this,
|
that = this,
|
||||||
diff, obj;
|
diff;
|
||||||
|
|
||||||
(function timer() {
|
(function timer() {
|
||||||
diff = that.duration - Math.floor((Date.now() - start) / 1000);
|
diff = that.duration - Math.floor((Date.now() - start) / 1000);
|
||||||
|
|
||||||
if (diff > 0) {
|
if (diff > 0) {
|
||||||
that.timeoutId = setTimeout(timer, that.granularity);
|
that.timeoutId = setTimeout(timer, that.granularity);
|
||||||
|
that.tick(diff);
|
||||||
obj = CountDownTimer.parse(diff);
|
|
||||||
that.onTickCallbacks.forEach(function(callback) {
|
|
||||||
callback.call(this, obj.minutes, obj.seconds);
|
|
||||||
}, that);
|
|
||||||
} else {
|
} else {
|
||||||
that.running = false;
|
that.running = false;
|
||||||
|
that.tick(0);
|
||||||
that.onExpireCallbacks.forEach(function(callback) {
|
that.expire();
|
||||||
callback.call(this);
|
|
||||||
}, that);
|
|
||||||
}
|
}
|
||||||
}());
|
}());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
CountDownTimer.prototype.tick = function(diff) {
|
||||||
|
var obj = CountDownTimer.parse(diff);
|
||||||
|
this.onTickCallbacks.forEach(function(callback) {
|
||||||
|
callback.call(this, obj.minutes, obj.seconds);
|
||||||
|
}, this);
|
||||||
|
}
|
||||||
|
CountDownTimer.prototype.expire = function() {
|
||||||
|
this.onExpireCallbacks.forEach(function(callback) {
|
||||||
|
callback.call(this);
|
||||||
|
}, this);
|
||||||
|
}
|
||||||
|
|
||||||
CountDownTimer.prototype.onTick = function(callback) {
|
CountDownTimer.prototype.onTick = function(callback) {
|
||||||
if (typeof callback === 'function') {
|
if (typeof callback === 'function') {
|
||||||
this.onTickCallbacks.push(callback);
|
this.onTickCallbacks.push(callback);
|
||||||
|
|
|
@ -37,7 +37,8 @@ var padmodals = (function()
|
||||||
$("#connectivity .visible").removeClass('visible');
|
$("#connectivity .visible").removeClass('visible');
|
||||||
$("#connectivity ."+messageId).addClass('visible');
|
$("#connectivity ."+messageId).addClass('visible');
|
||||||
|
|
||||||
automaticReconnect.showCountDownTimerToReconnectOnModal($('#connectivity .' + messageId));
|
var $modal = $('#connectivity .' + messageId);
|
||||||
|
automaticReconnect.showCountDownTimerToReconnectOnModal($modal, pad);
|
||||||
|
|
||||||
padeditbar.toggleDropDown("connectivity");
|
padeditbar.toggleDropDown("connectivity");
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue