Skip heartbeat if there is no response
This commit is contained in:
		
							parent
							
								
									351efff147
								
							
						
					
					
						commit
						ea893b2322
					
				@ -8,11 +8,18 @@
 | 
			
		||||
var Shynet = {
 | 
			
		||||
  idempotency: null,
 | 
			
		||||
  heartbeatTaskId: null,
 | 
			
		||||
  skipHeartbeat: false,
 | 
			
		||||
  sendHeartbeat: function () {
 | 
			
		||||
    try {
 | 
			
		||||
      if (document.hidden) {
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
      if (Shynet.skipHeartbeat) {
 | 
			
		||||
        console.warn("Heartbeat skipped due to lack of server response");
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      Shynet.skipHeartbeat = true;
 | 
			
		||||
      var xhr = new XMLHttpRequest();
 | 
			
		||||
      xhr.open(
 | 
			
		||||
        "POST",
 | 
			
		||||
@ -20,6 +27,12 @@ var Shynet = {
 | 
			
		||||
        true
 | 
			
		||||
      );
 | 
			
		||||
      xhr.setRequestHeader("Content-Type", "application/json");
 | 
			
		||||
      xhr.onload(function () { 
 | 
			
		||||
        Shynet.skipHeartbeat = false;
 | 
			
		||||
      });
 | 
			
		||||
      xhr.onerror(function () { 
 | 
			
		||||
        Shynet.skipHeartbeat = false;
 | 
			
		||||
      });
 | 
			
		||||
      xhr.send(
 | 
			
		||||
        JSON.stringify({
 | 
			
		||||
          idempotency: Shynet.idempotency,
 | 
			
		||||
@ -37,6 +50,7 @@ var Shynet = {
 | 
			
		||||
      clearInterval(Shynet.heartbeatTaskId);
 | 
			
		||||
    }
 | 
			
		||||
    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.sendHeartbeat();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user