changeset 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
files media/css/openwebchat.css media/js/openwebchat.js openwebchat.html
diffstat 3 files changed, 37 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/media/css/openwebchat.css	Thu Apr 30 18:34:30 2009 -0700
+++ b/media/css/openwebchat.css	Thu Apr 30 18:58:37 2009 -0700
@@ -54,11 +54,8 @@
     display: none;
 }
 
-#templates {
+.error {
     display: none;
-}
-
-.error {
     background: red;
     padding: 1em;
     margin-top: 1em;
--- 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();
   });
--- a/openwebchat.html	Thu Apr 30 18:34:30 2009 -0700
+++ b/openwebchat.html	Thu Apr 30 18:58:37 2009 -0700
@@ -11,17 +11,15 @@
 <div id="content">
 <h1>This is a conversation.</h1>
 <div id="incoming-messages"></div>
+<div class="error">
+  It appears that your connection to the server has been
+  severed. We'll try reconnecting in <span
+  class="seconds-left"></span> second<span class="plural">s</span>.
+</div>
 <textarea id="outgoing-message"></textarea>
 <p>Your name is <input type="text" id="name"/>.</p>
 <div id="bottom"></div>
 </div>
-<div id="templates">
-  <div class="error">
-    It appears that your connection to the server has been
-    severed. Please reload this page to reconnect. (Sorry, this will
-    be more user-friendly in the future!)
-  </div>
-</div>
 <script src="/media/js/jquery.js"></script>
 <script src="/media/js/json2.js"></script>
 <script src="/media/js/openwebchat.js"></script>