Skip heartbeat if there is no response
Fix xhr callbacks
This commit is contained in:
parent
073bd94112
commit
40cb5afbad
@ -8,11 +8,18 @@
|
|||||||
var Shynet = {
|
var Shynet = {
|
||||||
idempotency: null,
|
idempotency: null,
|
||||||
heartbeatTaskId: null,
|
heartbeatTaskId: null,
|
||||||
|
skipHeartbeat: false,
|
||||||
sendHeartbeat: function () {
|
sendHeartbeat: function () {
|
||||||
try {
|
try {
|
||||||
if (document.hidden) {
|
if (document.hidden) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (Shynet.skipHeartbeat) {
|
||||||
|
console.warn("Heartbeat skipped due to lack of server response");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Shynet.skipHeartbeat = true;
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
xhr.open(
|
xhr.open(
|
||||||
"POST",
|
"POST",
|
||||||
@ -20,6 +27,12 @@ var Shynet = {
|
|||||||
true
|
true
|
||||||
);
|
);
|
||||||
xhr.setRequestHeader("Content-Type", "application/json");
|
xhr.setRequestHeader("Content-Type", "application/json");
|
||||||
|
xhr.onload = function () {
|
||||||
|
Shynet.skipHeartbeat = false;
|
||||||
|
};
|
||||||
|
xhr.onerror = function () {
|
||||||
|
Shynet.skipHeartbeat = false;
|
||||||
|
};
|
||||||
xhr.send(
|
xhr.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
idempotency: Shynet.idempotency,
|
idempotency: Shynet.idempotency,
|
||||||
@ -30,13 +43,14 @@ var Shynet = {
|
|||||||
window.performance.timing.navigationStart,
|
window.performance.timing.navigationStart,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
} catch (e) { }
|
} catch (e) {}
|
||||||
},
|
},
|
||||||
newPageLoad: function () {
|
newPageLoad: function () {
|
||||||
if (Shynet.heartbeatTaskId != null) {
|
if (Shynet.heartbeatTaskId != null) {
|
||||||
clearInterval(Shynet.heartbeatTaskId);
|
clearInterval(Shynet.heartbeatTaskId);
|
||||||
}
|
}
|
||||||
Shynet.idempotency = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
Shynet.idempotency = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
||||||
|
Shynet.skipHeartbeat = false;
|
||||||
Shynet.heartbeatTaskId = setInterval(Shynet.sendHeartbeat, parseInt("{{heartbeat_frequency}}"));
|
Shynet.heartbeatTaskId = setInterval(Shynet.sendHeartbeat, parseInt("{{heartbeat_frequency}}"));
|
||||||
Shynet.sendHeartbeat();
|
Shynet.sendHeartbeat();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user