Merge pull request #382 from BeaudanBrown/delay-connectivity

Delay disconnected status and stop emitting empty events
pull/384/head
Beaudan Campbell-Brown 6 years ago committed by GitHub
commit 93993078c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,6 +6,8 @@
window.Whisper = window.Whisper || {}; window.Whisper = window.Whisper || {};
const DISCONNECTED_DELAY = 30000;
Whisper.NetworkStatusView = Whisper.View.extend({ Whisper.NetworkStatusView = Whisper.View.extend({
className: 'network-status', className: 'network-status',
templateName: 'networkStatus', templateName: 'networkStatus',
@ -27,6 +29,7 @@
this.model = new Backbone.Model(); this.model = new Backbone.Model();
this.listenTo(this.model, 'change', this.onChange); this.listenTo(this.model, 'change', this.onChange);
this.connectedTimer = null;
}, },
onReconnectTimer() { onReconnectTimer() {
this.setSocketReconnectInterval(60000); this.setSocketReconnectInterval(60000);
@ -43,7 +46,7 @@
getSocketStatus() { getSocketStatus() {
return window.getSocketStatus(); return window.getSocketStatus();
}, },
getNetworkStatus() { getNetworkStatus(shortCircuit = false) {
let message = ''; let message = '';
let instructions = ''; let instructions = '';
let hasInterruption = false; let hasInterruption = false;
@ -55,21 +58,37 @@
case WebSocket.CONNECTING: case WebSocket.CONNECTING:
message = i18n('connecting'); message = i18n('connecting');
this.setSocketReconnectInterval(null); this.setSocketReconnectInterval(null);
window.clearTimeout(this.connectedTimer);
this.connectedTimer = null;
break; break;
case WebSocket.OPEN: case WebSocket.OPEN:
this.setSocketReconnectInterval(null); this.setSocketReconnectInterval(null);
window.clearTimeout(this.connectedTimer);
this.connectedTimer = null;
break; break;
case WebSocket.CLOSED: case WebSocket.CLOSED:
message = i18n('disconnected'); // Intentional fallthrough
instructions = i18n('checkNetworkConnection');
hasInterruption = true;
break;
case WebSocket.CLOSING: case WebSocket.CLOSING:
default: // Intentional fallthrough
message = i18n('disconnected'); default: {
instructions = i18n('checkNetworkConnection'); const markOffline = () => {
hasInterruption = true; message = i18n('disconnected');
instructions = i18n('checkNetworkConnection');
hasInterruption = true;
};
if (shortCircuit) {
// Used to skip the timer for testing
markOffline();
break;
}
if (!this.connectedTimer) {
// Mark offline if disconnected for 30 seconds
this.connectedTimer = window.setTimeout(() => {
markOffline();
}, DISCONNECTED_DELAY);
}
break; break;
}
} }
if ( if (

@ -83,8 +83,7 @@
} }
}; };
// Note: calling callback(false) is currently not necessary this.pollServer = async () => {
this.pollServer = async callback => {
// This blocking call will return only when all attempts // This blocking call will return only when all attempts
// at reaching snodes are exhausted or a DNS error occured // at reaching snodes are exhausted or a DNS error occured
try { try {
@ -93,7 +92,6 @@
stopPolling, stopPolling,
messages => { messages => {
connected = true; connected = true;
callback(connected);
messages.forEach(message => { messages.forEach(message => {
const { data } = message; const { data } = message;
this.handleMessage(data); this.handleMessage(data);
@ -111,7 +109,7 @@
connected = false; connected = false;
// Exhausted all our snodes urls, trying again later from scratch // Exhausted all our snodes urls, trying again later from scratch
setTimeout(() => { setTimeout(() => {
this.pollServer(callback); this.pollServer();
}, EXHAUSTED_SNODES_RETRY_DELAY); }, EXHAUSTED_SNODES_RETRY_DELAY);
}; };

@ -73,16 +73,7 @@ MessageReceiver.prototype.extend({
this.httpPollingResource = new HttpResource(lokiMessageAPI, { this.httpPollingResource = new HttpResource(lokiMessageAPI, {
handleRequest: this.handleRequest.bind(this), handleRequest: this.handleRequest.bind(this),
}); });
this.httpPollingResource.pollServer(connected => { this.httpPollingResource.pollServer();
// Emulate receiving an 'empty' websocket messages from the server.
// This is required to update the internal logic that checks
// if we are connected to the server. Without this, for example,
// the loading screen would never disappear if the navigator
// detects internet connectivity but never receives an 'empty' signal.
if (connected) {
this.onEmpty();
}
});
localLokiServer.on('message', this.handleP2pMessage.bind(this)); localLokiServer.on('message', this.handleP2pMessage.bind(this));
this.startLocalServer(); this.startLocalServer();

@ -153,7 +153,8 @@ describe('NetworkStatusView', () => {
it('should be interrupted', () => { it('should be interrupted', () => {
socketStatus = socketStatusVal; socketStatus = socketStatusVal;
networkStatusView.update(); networkStatusView.update();
const status = networkStatusView.getNetworkStatus(); const shortCircuit = true;
const status = networkStatusView.getNetworkStatus(shortCircuit);
assert(status.hasInterruption); assert(status.hasInterruption);
}); });
}); });

Loading…
Cancel
Save