Added ability to gracefully fail the client if the connection to the server fails
authorDan
Tue, 01 Jul 2008 04:16:04 -0400
changeset 22 3a4f0cd0794e
parent 21 74edc873234f
child 23 08225c2eb0b6
Added ability to gracefully fail the client if the connection to the server fails
scripts/ajax.js
--- a/scripts/ajax.js	Mon Jun 30 12:36:13 2008 -0400
+++ b/scripts/ajax.js	Tue Jul 01 04:16:04 2008 -0400
@@ -49,6 +49,9 @@
 
 function ajaxGet(uri, f)
 {
+  if ( ajax_panicked )
+    return false;
+  
   if (window.XMLHttpRequest)
   {
     ajax = new XMLHttpRequest();
@@ -72,6 +75,9 @@
 
 function ajaxPost(uri, parms, f)
 {
+  if ( ajax_panicked )
+    return false;
+  
   if (window.XMLHttpRequest)
   {
     ajax = new XMLHttpRequest();
@@ -97,6 +103,10 @@
     ajax.setRequestHeader("Content-length", parms.length);
   }
   ajax.setRequestHeader("Connection", "close");
+  ajax.onerror = function()
+  {
+    ajax_panic();
+  }
   ajax.send(parms);
 }
 
@@ -204,9 +214,51 @@
           jump_current_track();
         }
       }
+      else if ( ajax.readyState == 4 && ajax.status != 200 )
+      {
+        ajax_panic();
+        console.debug(ajax);
+      }
     });
 }
 
+var ajax_panicked = false;
+
+function ajax_panic()
+{
+  // set error flag
+  ajax_panicked = true;
+  
+  // scroll to the top
+  window.scroll(0, 0);
+  
+  // stop events
+  pulsar_reset();
+  window.clearInterval(pl_refresh_id);
+  if ( ct_counter )
+    window.clearInterval(ct_counter);
+  if ( pulsar_interval_id )
+    window.clearInterval(pulsar_interval_id);
+  
+  // show error message
+  var floater = document.createElement('div');
+  floater.style.backgroundColor = '#ffffff';
+  floater.style.opacity = 0.7;
+  floater.style.filter = 'alpha(opacity=70)';
+  floater.style.textAlign = 'center';
+  floater.style.paddingTop = '120px';
+  floater.style.position = 'fixed';
+  floater.style.zIndex = '999';
+  floater.style.width = '100%';
+  floater.style.height = '100%';
+  floater.style.top = '0px';
+  floater.style.left = '0px';
+  floater.style.color = '#000000';
+  floater.innerHTML = 'There was a problem with a refresh request to the server. Please reload the page.';
+  var body = document.getElementsByTagName('body')[0];
+  body.appendChild(floater);
+}
+
 function player_action(action)
 {
   var act2 = action;
@@ -326,7 +378,7 @@
   return str;
 }
 
-setInterval(refresh_playlist, 10000);
+var pl_refresh_id = setInterval(refresh_playlist, 10000);
 
 window.onload = function(e)
 {
@@ -414,7 +466,7 @@
 }
 
 addOnloadHook(pulsar_reset);
-setInterval(pulsar_advance, 50);
+var pulsar_interval_id = setInterval(pulsar_advance, 50);
 
 function updateTitle(artist, album, track)
 {