[lib.content-controller] managed connection status bar + limited attempts when online + unlimited offline (navigator.onLine) + increasing timeout for attempts
This commit is contained in:
parent
fb02ba5407
commit
19388a8802
|
@ -3,8 +3,22 @@ export default class ContentController{
|
|||
/* (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
|
||||
*
|
||||
|
@ -156,10 +170,14 @@ export default class ContentController{
|
|||
|
||||
// 3. Bind events
|
||||
csock.onconnected = () => {
|
||||
|
||||
// show connection status
|
||||
gs.get.connection = 2;
|
||||
setTimeout( () => {
|
||||
gs.get.connection = null;
|
||||
}, 3000);
|
||||
|
||||
// reset attempt count
|
||||
gs.get.content.attempt.count = 0;
|
||||
|
||||
setTimeout( () => { gs.get.connection = null; }, 1500);
|
||||
};
|
||||
csock.onreceive = gs.get.content.ws_handler.bind({ event: 'receive' });
|
||||
csock.onclose = gs.get.content.ws_handler.bind({ event: 'close' });
|
||||
|
@ -186,11 +204,21 @@ export default class ContentController{
|
|||
return;
|
||||
|
||||
|
||||
/* (2) CLOSE event -> reconnect in 500ms
|
||||
/* (2) CLOSE event -> reconnect
|
||||
---------------------------------------------------------*/
|
||||
if( this.event === 'close' ){
|
||||
// 1. update connection status bar
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue