IMPR: Ping online/offline handling

This commit is contained in:
Tony Air 2020-09-11 01:54:43 +07:00
parent 87ad23efed
commit bcc4a131ce

View File

@ -89,6 +89,33 @@ const MainUI = (($) => {
}
});
// session ping
const sessionPing = () => {
if ($Body.hasClass('is-offline')) {
return;
}
console.log(`${NAME}: session ping`);
$.ajax({
sync: false,
async: true,
cache: false,
url: '/Security/ping',
global: false,
type: 'POST',
complete: (data, datastatus) => {
if (datastatus !== 'success') {
console.warn(`${NAME}: ping failed`);
clearInterval(pingInterval);
}
updateOnlineStatus();
},
});
};
let pingInterval = setInterval(sessionPing, 300000); // 5 min in ms
// update online/offline state
const updateOnlineStatus = () => {
if (typeof navigator.onLine === 'undefined') {
@ -96,11 +123,15 @@ const MainUI = (($) => {
}
if (!navigator.onLine) {
console.log(`${NAME}: Tab: offline`);
clearInterval(pingInterval);
$Body.addClass('is-offline');
$Body.trigger(Events.OFFLINE);
$W.trigger(Events.OFFLINE);
} else {
console.log(`${NAME}: Tab: online`);
pingInterval = setInterval(sessionPing, 300000); // 5 min in ms
$Body.removeClass('is-offline');
$Body.trigger(Events.ONLINE);
$W.trigger(Events.ONLINE);
@ -143,36 +174,6 @@ const MainUI = (($) => {
});
};
// session ping
const pingInterval = setInterval(() => {
if ($Body.hasClass('is-offline')) {
return;
}
$.ajax({
sync: false,
async: true,
cache: false,
url: '/Security/ping',
global: false,
type: 'POST',
complete(data, datastatus) {
if (datastatus !== 'success') {
clearInterval(pingInterval);
$Body.addClass('is-offline');
$Body.trigger(Events.OFFLINE);
$W.trigger(Events.OFFLINE);
//W.location.reload(false);
} else {
$Body.removeClass('is-offline');
$Body.trigger(Events.ONLINE);
$W.trigger(Events.ONLINE);
}
},
});
}, 300000); // 5 min in ms
W.URLDetails = {
base: $('base').attr('href'),
relative: '/',