Disconnect from socket faster on loss of network access (#1512)
* Disconnect from socket faster on complete loss of network access Today we wait for a keepalive request to fail; this change forces disconnect in the case that the browser tells us that we're now offline. FREEBIE * MessageReceiver: don't react to errors after explicit close() FREEBIE
This commit is contained in:
parent
b0f9644c14
commit
6323f598b5
3 changed files with 41 additions and 8 deletions
|
@ -149,9 +149,9 @@
|
||||||
Whisper.events.on('start-shutdown', function() {
|
Whisper.events.on('start-shutdown', function() {
|
||||||
if (messageReceiver) {
|
if (messageReceiver) {
|
||||||
messageReceiver.close().then(function() {
|
messageReceiver.close().then(function() {
|
||||||
messageReceiver = null;
|
|
||||||
Whisper.events.trigger('shutdown-complete');
|
Whisper.events.trigger('shutdown-complete');
|
||||||
});
|
});
|
||||||
|
messageReceiver = null;
|
||||||
} else {
|
} else {
|
||||||
Whisper.events.trigger('shutdown-complete');
|
Whisper.events.trigger('shutdown-complete');
|
||||||
}
|
}
|
||||||
|
@ -159,11 +159,15 @@
|
||||||
|
|
||||||
function connect(firstRun) {
|
function connect(firstRun) {
|
||||||
window.removeEventListener('online', connect);
|
window.removeEventListener('online', connect);
|
||||||
|
window.addEventListener('offline', disconnect);
|
||||||
|
|
||||||
if (!Whisper.Registration.everDone()) { return; }
|
if (!Whisper.Registration.everDone()) { return; }
|
||||||
if (Whisper.Import.isIncomplete()) { return; }
|
if (Whisper.Import.isIncomplete()) { return; }
|
||||||
|
|
||||||
if (messageReceiver) { messageReceiver.close(); }
|
if (messageReceiver) {
|
||||||
|
messageReceiver.close();
|
||||||
|
messageReceiver = null;
|
||||||
|
}
|
||||||
|
|
||||||
var USERNAME = storage.get('number_id');
|
var USERNAME = storage.get('number_id');
|
||||||
var PASSWORD = storage.get('password');
|
var PASSWORD = storage.get('password');
|
||||||
|
@ -439,6 +443,17 @@
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function disconnect() {
|
||||||
|
window.removeEventListener('offline', disconnect);
|
||||||
|
window.addEventListener('online', connect);
|
||||||
|
|
||||||
|
console.log('offline');
|
||||||
|
if (messageReceiver) {
|
||||||
|
messageReceiver.close();
|
||||||
|
messageReceiver = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function onError(ev) {
|
function onError(ev) {
|
||||||
var error = ev.error;
|
var error = ev.error;
|
||||||
console.log(error);
|
console.log(error);
|
||||||
|
@ -457,10 +472,6 @@
|
||||||
setTimeout(connect, 60000);
|
setTimeout(connect, 60000);
|
||||||
|
|
||||||
Whisper.events.trigger('reconnectTimer');
|
Whisper.events.trigger('reconnectTimer');
|
||||||
} else {
|
|
||||||
console.log('offline');
|
|
||||||
if (messageReceiver) { messageReceiver.close(); }
|
|
||||||
window.addEventListener('online', connect);
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -38394,6 +38394,7 @@ MessageReceiver.prototype.extend({
|
||||||
this.incoming = [this.pending];
|
this.incoming = [this.pending];
|
||||||
},
|
},
|
||||||
close: function() {
|
close: function() {
|
||||||
|
this.calledClose = true;
|
||||||
this.socket.close(3000, 'called close');
|
this.socket.close(3000, 'called close');
|
||||||
return this.drain();
|
return this.drain();
|
||||||
},
|
},
|
||||||
|
@ -38407,7 +38408,17 @@ MessageReceiver.prototype.extend({
|
||||||
return Promise.all(this.dispatchEvent(event));
|
return Promise.all(this.dispatchEvent(event));
|
||||||
},
|
},
|
||||||
onclose: function(ev) {
|
onclose: function(ev) {
|
||||||
console.log('websocket closed', ev.code, ev.reason || '');
|
console.log(
|
||||||
|
'websocket closed',
|
||||||
|
ev.code,
|
||||||
|
ev.reason || '',
|
||||||
|
'calledClose:',
|
||||||
|
this.calledClose
|
||||||
|
);
|
||||||
|
|
||||||
|
if (this.calledClose) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (ev.code === 3000) {
|
if (ev.code === 3000) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,7 @@ MessageReceiver.prototype.extend({
|
||||||
this.incoming = [this.pending];
|
this.incoming = [this.pending];
|
||||||
},
|
},
|
||||||
close: function() {
|
close: function() {
|
||||||
|
this.calledClose = true;
|
||||||
this.socket.close(3000, 'called close');
|
this.socket.close(3000, 'called close');
|
||||||
return this.drain();
|
return this.drain();
|
||||||
},
|
},
|
||||||
|
@ -53,7 +54,17 @@ MessageReceiver.prototype.extend({
|
||||||
return Promise.all(this.dispatchEvent(event));
|
return Promise.all(this.dispatchEvent(event));
|
||||||
},
|
},
|
||||||
onclose: function(ev) {
|
onclose: function(ev) {
|
||||||
console.log('websocket closed', ev.code, ev.reason || '');
|
console.log(
|
||||||
|
'websocket closed',
|
||||||
|
ev.code,
|
||||||
|
ev.reason || '',
|
||||||
|
'calledClose:',
|
||||||
|
this.calledClose
|
||||||
|
);
|
||||||
|
|
||||||
|
if (this.calledClose) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (ev.code === 3000) {
|
if (ev.code === 3000) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue