From 7e9016f51066595bffef789e5c5c73044545bd12 Mon Sep 17 00:00:00 2001 From: sachaaaaa Date: Wed, 14 Nov 2018 11:32:37 +1100 Subject: [PATCH] Make network status warning work with HTTP polling --- libtextsecure/http-resources.js | 12 ++++++- libtextsecure/message_receiver.js | 57 +++++++++++++++++++------------ 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/libtextsecure/http-resources.js b/libtextsecure/http-resources.js index 8dc4212a1..7627e9b23 100644 --- a/libtextsecure/http-resources.js +++ b/libtextsecure/http-resources.js @@ -47,17 +47,23 @@ if (typeof handleRequest !== 'function') { handleRequest = request => request.respond(404, 'Not found'); }; + let connected = false; - this.startPolling = async function pollServer() { + this.startPolling = async function pollServer(callBack) { const myKeys = await textsecure.storage.protocol.getIdentityKeyPair(); const pubKey = StringView.arrayBufferToHex(myKeys.pubKey) let result; try { result = await server.retrieveMessages(pubKey); + connected = true; } catch(err) { + connected = false; setTimeout(() => { pollServer(); }, 5000); return; } + if (typeof handleRequest === 'function') { + callBack(connected); + } if (!result.messages) { setTimeout(() => { pollServer(); }, 5000); return; @@ -79,5 +85,9 @@ }); setTimeout(() => { pollServer(); }, 5000); }; + + this.isConnected = function isConnected() { + return connected; + } }; })(); diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index e70ba299c..2bcced63b 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -166,33 +166,43 @@ MessageReceiver.prototype.extend({ } this.hasConnected = true; - this.hr = new HttpResource(this.lokiserver, { + this.httpPollingResource = new HttpResource(this.lokiserver, { handleRequest: this.handleRequest.bind(this), }); - this.hr.startPolling(); + this.httpPollingResource.startPolling((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(); + } + }); // TODO: Rework this socket stuff to work with online messaging - return; + const useWebSocket = false; + if (useWebSocket) { + if (this.socket && this.socket.readyState !== WebSocket.CLOSED) { + this.socket.close(); + this.wsr.close(); + } + // initialize the socket and start listening for messages + this.socket = this.server.getMessageSocket(); + this.socket.onclose = this.onclose.bind(this); + this.socket.onerror = this.onerror.bind(this); + this.socket.onopen = this.onopen.bind(this); + this.wsr = new WebSocketResource(this.socket, { + handleRequest: this.handleRequest.bind(this), + keepalive: { + path: '/v1/keepalive', + disconnect: true, + }, + }); - if (this.socket && this.socket.readyState !== WebSocket.CLOSED) { - this.socket.close(); - this.wsr.close(); + // Because sometimes the socket doesn't properly emit its close event + this._onClose = this.onclose.bind(this); + this.wsr.addEventListener('close', this._onClose); } - // initialize the socket and start listening for messages - this.socket = this.server.getMessageSocket(); - this.socket.onclose = this.onclose.bind(this); - this.socket.onerror = this.onerror.bind(this); - this.socket.onopen = this.onopen.bind(this); - this.wsr = new WebSocketResource(this.socket, { - handleRequest: this.handleRequest.bind(this), - keepalive: { - path: '/v1/keepalive', - disconnect: true, - }, - }); - - // Because sometimes the socket doesn't properly emit its close event - this._onClose = this.onclose.bind(this); - this.wsr.addEventListener('close', this._onClose); // Ensures that an immediate 'empty' event from the websocket will fire only after // all cached envelopes are processed. @@ -609,6 +619,9 @@ MessageReceiver.prototype.extend({ throw new Error('Received message with no content and no legacyMessage'); }, getStatus() { + if (this.httpPollingResource) { + return this.httpPollingResource.isConnected() ? WebSocket.OPEN : WebSocket.CLOSED; + } if (this.socket) { return this.socket.readyState; } else if (this.hasConnected) {