diff lib/js/urweb.js @ 673:a8effb6159c2

Variable timeouts and client keep-alive
author Adam Chlipala <adamc@hcoop.net>
date Tue, 24 Mar 2009 15:35:46 -0400
parents df6eb58de040
children 44f23712020d
line wrap: on
line diff
--- a/lib/js/urweb.js	Tue Mar 24 15:05:28 2009 -0400
+++ b/lib/js/urweb.js	Tue Mar 24 15:35:46 2009 -0400
@@ -141,6 +141,7 @@
 var client_id = 0;
 var client_pass = 0;
 var url_prefix = "/";
+var timeout = 60;
 
 function getXHR(uri)
 {
@@ -234,8 +235,18 @@
 function listener() {
   var uri = path_join(url_prefix, ".msgs");
   var xhr = getXHR();
-  var orsc = function() {
+  var tid, orsc, onTimeout;
+
+  var connect = function () {
+    xhr.onreadystatechange = orsc;
+    tid = window.setTimeout(onTimeout, timeout * 500);
+    requestUri(xhr, uri);
+  }
+
+  orsc = function() {
     if (xhr.readyState == 4) {
+      window.clearTimeout(tid);
+
       var isok = false;
 
       try {
@@ -271,8 +282,7 @@
           }
         }
 
-        xhr.onreadystatechange = orsc;
-        requestUri(xhr, uri);
+        connect();
       }
       else {
         try {
@@ -282,8 +292,12 @@
     }
   };
 
-  xhr.onreadystatechange = orsc;
-  requestUri(xhr, uri);
+  onTimeout = function() {
+    xhr.abort();
+    connect();
+  };
+
+  connect();
 }
 
 function rv(chn, parse, k) {