[lib.content-controller] managed connection status bar + limited attempts when online + unlimited offline (navigator.onLine) + increasing timeout for attempts

This commit is contained in:
xdrm-brackets 2018-04-04 15:22:34 +02:00
parent fb02ba5407
commit 19388a8802
1 changed files with 34 additions and 6 deletions

View File

@ -3,8 +3,22 @@ export default class ContentController{
/* (1) Construct default attributes /* (1) Construct default attributes
* *
---------------------------------------------------------*/ ---------------------------------------------------------*/
constructor(){
constructor(){} /* (1) Websocket re-connection policy */
this.attempt = {
max: 3,
count: 0,
_default_timeout: 500, // in ms
get timeout(){
// return timeout + increment it for next time
return this._default_timeout * Math.pow(2, this.count++);
}
};
}
/* (2) Channel bindings /* (2) Channel bindings
* *
@ -156,10 +170,14 @@ export default class ContentController{
// 3. Bind events // 3. Bind events
csock.onconnected = () => { csock.onconnected = () => {
// show connection status
gs.get.connection = 2; gs.get.connection = 2;
setTimeout( () => {
gs.get.connection = null; // reset attempt count
}, 3000); gs.get.content.attempt.count = 0;
setTimeout( () => { gs.get.connection = null; }, 1500);
}; };
csock.onreceive = gs.get.content.ws_handler.bind({ event: 'receive' }); csock.onreceive = gs.get.content.ws_handler.bind({ event: 'receive' });
csock.onclose = gs.get.content.ws_handler.bind({ event: 'close' }); csock.onclose = gs.get.content.ws_handler.bind({ event: 'close' });
@ -186,11 +204,21 @@ export default class ContentController{
return; return;
/* (2) CLOSE event -> reconnect in 500ms /* (2) CLOSE event -> reconnect
---------------------------------------------------------*/ ---------------------------------------------------------*/
if( this.event === 'close' ){ if( this.event === 'close' ){
// 1. update connection status bar
gs.get.connection = 0; gs.get.connection = 0;
return setTimeout(gs.get.content.ws_connect.bind(gs.get.content), 500);
// 2. Check connection offline
let online = navigator.onLine;
// 3. do not reconnect if max attempt exceeded (and if not offline of course)
if( online && gs.get.content.attempt.count >= gs.get.content.attempt.max-1 )
return;
// 4. Try to reconnect
return setTimeout(gs.get.content.ws_connect.bind(gs.get.content), gs.get.content.attempt.timeout);
} }