diff media/js/openwebchat.js @ 80:002868ce7212

We now try to reconnect after 30 seconds if we get disconnected.
author Atul Varma <varmaa@toolness.com>
date Thu, 30 Apr 2009 18:58:37 -0700
parents 91f31d2030b9
children 2fc520114ed4
line wrap: on
line diff
--- a/media/js/openwebchat.js	Thu Apr 30 18:34:30 2009 -0700
+++ b/media/js/openwebchat.js	Thu Apr 30 18:58:37 2009 -0700
@@ -224,16 +224,35 @@
       onMessage(convStorage.get(i), $('#incoming-messages'));
     $('#incoming-messages').slideDown();
 
-    OpenWebChat.listenForMessages(
-      {storage: convStorage,
-       onMessages: onMessages,
-       onError: function onError(exception) {
-         if (window.console)
-           window.console.log('The error', exception, 'occurred.');
-         var error = $('#templates .error').clone();
-         error.hide();
-         $('#incoming-messages').append(error);
-         error.slideDown();
-       }
-      });
+    var RECONNECT_SECONDS = 30;
+
+    function startListening() {
+      OpenWebChat.listenForMessages(
+        {storage: convStorage,
+         onMessages: onMessages,
+         onError: function onError(exception) {
+           var timeLeft = RECONNECT_SECONDS;
+           if (window.console)
+             window.console.log('The error', exception, 'occurred.');
+           $('.error .seconds-left').text(timeLeft);
+           $('.error .plural').show();
+           $('.error').slideDown();
+           var intervalId = window.setInterval(
+             function() {
+               timeLeft -= 1;
+               if (timeLeft == 1)
+                 $('.error .plural').hide();
+               if (!timeLeft) {
+                 window.clearInterval(intervalId);
+                 $('.error').slideUp(startListening);
+               } else
+                 $('.error .seconds-left').text(timeLeft);
+             },
+             1000
+           );
+         }
+        });
+    }
+
+    startListening();
   });