E573 Replace interval with chainged setTimeout + only allow last conn hear… · ServiceStack/ServiceStack@2f0b38d · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f0b38d

Browse files
committed
Replace interval with chainged setTimeout + only allow last conn heartbeat through
1 parent ab407f4 commit 2f0b38d

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

src/ServiceStack/js/ss-utils.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -703,30 +703,34 @@
703703
if (cmd === "onConnect") {
704704
$.extend(opt, msg);
705705
if (opt.heartbeatUrl) {
706-
if (opt.heartbeat) {
707-
window.clearInterval(opt.heartbeat);
708-
}
709-
opt.heartbeat = window.setInterval(function () {
710-
if ($.ss.eventSource.readyState === 2) //CLOSED
711-
{
712-
window.clearInterval(opt.heartbeat);
713-
var stopFn = $.ss.handlers["onStop"];
714-
if (stopFn != null)
715-
stopFn.apply($.ss.eventSource);
716-
$.ss.reconnectServerEvents({ errorArgs: { error:'CLOSED' } });
717-
return;
718-
}
719-
$.ajax({
720-
type: "POST",
721-
url: opt.heartbeatUrl,
722-
data: null,
723-
dataType: "text",
724-
success: function (r) { },
725-
error: function () {
726-
$.ss.reconnectServerEvents({ errorArgs: arguments });
706+
$.ss.CONNECT_ID = $.ss.CONNECT_ID ? $.ss.CONNECT_ID + 1 : 1;
707+
(function(connectId) {
708+
function sendHeartbeat() {
709+
if (connectId !== $.ss.CONNECT_ID) // Only allow latest connections heartbeat callback through
710+
return;
711+
if ($.ss.eventSource.readyState === 2) //CLOSED
712+
{
713+
var stopFn = $.ss.handlers["onStop"];
714+
if (stopFn != null)
715+
stopFn.apply($.ss.eventSource);
716+
$.ss.reconnectServerEvents({ errorArgs: { error:'CLOSED' } });
717+
return;
727718
}
728-
});
729-
}, parseInt(opt.heartbeatIntervalMs) || 10000);
719+
$.ajax({
720+
type: "POST",
721+
url: opt.heartbeatUrl,
722+
data: null,
723+
dataType: "text",
724+
success: function (r) {
725+
setTimeout(sendHeartbeat, parseInt(opt.heartbeatIntervalMs) || 10000)
726+
},
727+
error: function () {
728+
$.ss.reconnectServerEvents({ errorArgs: arguments });
729+
}
730+
});
731+
}
732+
setTimeout(sendHeartbeat, parseInt(opt.heartbeatIntervalMs) || 10000);
733+
})($.ss.CONNECT_ID);
730734
}
731735
if (opt.unRegisterUrl) {
732736
$(window).on("unload", function () {

0 commit comments

Comments
 (0)
0