From 7dd155eb163f981983a80606e7ad0936bdb75ef8 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Wed, 7 Aug 2019 11:43:01 +1000 Subject: [PATCH 1/2] Remove empty event trigger, doesn't seem to be required any more but could be wrong --- js/views/network_status_view.js | 29 +++++++++++++++++++++++------ libtextsecure/http-resources.js | 6 ++---- libtextsecure/message_receiver.js | 11 +---------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/js/views/network_status_view.js b/js/views/network_status_view.js index 54fa25700..d637de028 100644 --- a/js/views/network_status_view.js +++ b/js/views/network_status_view.js @@ -6,6 +6,8 @@ window.Whisper = window.Whisper || {}; + const DISCONNECTED_DELAY = 30000; + Whisper.NetworkStatusView = Whisper.View.extend({ className: 'network-status', templateName: 'networkStatus', @@ -27,6 +29,7 @@ this.model = new Backbone.Model(); this.listenTo(this.model, 'change', this.onChange); + this.connectedTimer = null; }, onReconnectTimer() { this.setSocketReconnectInterval(60000); @@ -55,20 +58,34 @@ case WebSocket.CONNECTING: message = i18n('connecting'); this.setSocketReconnectInterval(null); + window.clearTimeout(this.connectedTimer); + this.connectedTimer = null; break; case WebSocket.OPEN: this.setSocketReconnectInterval(null); + window.clearTimeout(this.connectedTimer); + this.connectedTimer = null; break; case WebSocket.CLOSED: - message = i18n('disconnected'); - instructions = i18n('checkNetworkConnection'); - hasInterruption = true; + if (!this.connectedTimer) { + // Mark offline if disconnected for 30 seconds + this.connectedTimer = window.setTimeout(() => { + message = i18n('disconnected'); + instructions = i18n('checkNetworkConnection'); + hasInterruption = true; + }, DISCONNECTED_DELAY); + } break; case WebSocket.CLOSING: default: - message = i18n('disconnected'); - instructions = i18n('checkNetworkConnection'); - hasInterruption = true; + if (!this.connectedTimer) { + // Mark offline if disconnected for 30 seconds + this.connectedTimer = window.setTimeout(() => { + message = i18n('disconnected'); + instructions = i18n('checkNetworkConnection'); + hasInterruption = true; + }, DISCONNECTED_DELAY); + } break; } diff --git a/libtextsecure/http-resources.js b/libtextsecure/http-resources.js index 38ae9b6a3..2e2b42a19 100644 --- a/libtextsecure/http-resources.js +++ b/libtextsecure/http-resources.js @@ -83,8 +83,7 @@ } }; - // Note: calling callback(false) is currently not necessary - this.pollServer = async callback => { + this.pollServer = async () => { // This blocking call will return only when all attempts // at reaching snodes are exhausted or a DNS error occured try { @@ -93,7 +92,6 @@ stopPolling, messages => { connected = true; - callback(connected); messages.forEach(message => { const { data } = message; this.handleMessage(data); @@ -111,7 +109,7 @@ connected = false; // Exhausted all our snodes urls, trying again later from scratch setTimeout(() => { - this.pollServer(callback); + this.pollServer(); }, EXHAUSTED_SNODES_RETRY_DELAY); }; diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 720a2613f..9c356df08 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -73,16 +73,7 @@ MessageReceiver.prototype.extend({ this.httpPollingResource = new HttpResource(lokiMessageAPI, { handleRequest: this.handleRequest.bind(this), }); - this.httpPollingResource.pollServer(connected => { - // 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(); - } - }); + this.httpPollingResource.pollServer(); localLokiServer.on('message', this.handleP2pMessage.bind(this)); this.startLocalServer(); From ccfc9046b4c9d6b7cf0640af6ed100be4c2a71f5 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Wed, 7 Aug 2019 13:39:33 +1000 Subject: [PATCH 2/2] Fix tests --- js/views/network_status_view.js | 30 ++++++++++++++------------ test/views/network_status_view_test.js | 3 ++- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/js/views/network_status_view.js b/js/views/network_status_view.js index d637de028..de5034f19 100644 --- a/js/views/network_status_view.js +++ b/js/views/network_status_view.js @@ -46,7 +46,7 @@ getSocketStatus() { return window.getSocketStatus(); }, - getNetworkStatus() { + getNetworkStatus(shortCircuit = false) { let message = ''; let instructions = ''; let hasInterruption = false; @@ -67,26 +67,28 @@ this.connectedTimer = null; break; case WebSocket.CLOSED: - if (!this.connectedTimer) { - // Mark offline if disconnected for 30 seconds - this.connectedTimer = window.setTimeout(() => { - message = i18n('disconnected'); - instructions = i18n('checkNetworkConnection'); - hasInterruption = true; - }, DISCONNECTED_DELAY); - } - break; + // Intentional fallthrough case WebSocket.CLOSING: - default: + // Intentional fallthrough + default: { + const markOffline = () => { + 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(() => { - message = i18n('disconnected'); - instructions = i18n('checkNetworkConnection'); - hasInterruption = true; + markOffline(); }, DISCONNECTED_DELAY); } break; + } } if ( diff --git a/test/views/network_status_view_test.js b/test/views/network_status_view_test.js index a3daa7883..85cb64c68 100644 --- a/test/views/network_status_view_test.js +++ b/test/views/network_status_view_test.js @@ -153,7 +153,8 @@ describe('NetworkStatusView', () => { it('should be interrupted', () => { socketStatus = socketStatusVal; networkStatusView.update(); - const status = networkStatusView.getNetworkStatus(); + const shortCircuit = true; + const status = networkStatusView.getNetworkStatus(shortCircuit); assert(status.hasInterruption); }); });