Make network status warning work with HTTP polling

This commit is contained in:
sachaaaaa 2018-11-14 11:32:37 +11:00
parent cb46dd3adb
commit 7e9016f510
2 changed files with 46 additions and 23 deletions

View file

@ -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;
}
};
})();

View file

@ -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) {