$(document).ready(function() {
  if (!window.console) window.console = {};
  if (!window.console.log) window.console.log = function() {};

  console.log('ready');
  updater.start();
});

var updater = {

  socket: null,
  attempts: 1,

  start: function() {

    if (updater.socket === null) {
      var url = 'ws://' + location.host + '/ts';

      updater.socket = new WebSocket(url);
      updater.$message = $('#message');

      console.log(url);

      // onopen
      updater.socket.onopen = function() {
          console.log('onopen');

          updater.$message.attr('class', 'label label-success');
          updater.$message.text('open');

          // reset the tries back to 1 since we have a new connection opened.
          updater.attempts = 1;
        };
        // onopen

      // onmessage
      updater.socket.onmessage = function(event) {
        console.log('onmessage');

        updater.$message.attr('class', 'label label-primary');

        var json = JSON.parse(event.data);
        console.log(json);

        updater.$message.text('recieved');
      };

      // onclose
      updater.socket.onclose = function(event) {
        console.log('onclose. reason: %s', event.reason);

        updater.$message.attr('class', 'label label-important');
        updater.$message.text('closed');

        var time = updater.generateInterval(updater.attempts);

        setTimeout(function() {
          // We"ve tried to reconnect so increment the attempts by 1
          updater.attempts ++;
          console.log('attempts: ', updater.attempts);

          // Connection has closed so try to reconnect every 10 seconds.
          updater.socket = null;
          updater.start();

        }, time);
      };

      // onerror
      updater.socket.onerror = function(event) {
        console.log('onerror');

        updater.$message.attr('class', 'label label-warning');
        updater.$message.text('error occurred');
      };
    }
  },

  generateInterval: function(k) {
    // generate the interval to a random number between 0 and the max
    return Math.min(30, (Math.pow(2, k) - 1)) * 1000 * Math.random();
  }
};

@@ -213,18 +213,50 @@
 : null,%0A
+%0A
   
+retry_attempts: 0,%0A  max_retry_
 attempts
@@ -258,16 +258,18 @@
 empts: 1
+20
 ,%0A%0A  sta
@@ -690,140 +690,10 @@
     
-    // reset the tries back to 1 since we have a new connection opened.%0A          updater.attempts = 1;%0A        %7D;%0A        // onopen
+%7D;
 %0A%0A  
@@ -968,24 +968,139 @@
 'recieved');
+%0A%0A        // reset the tries back to 0 since we have a new connection opened.%0A        updater.retry_attempts = 0;%0A%0A
 %0A      %7D;%0A%0A 
@@ -1342,52 +1342,55 @@
     
-var time = updater.generateInterval(updater.
+if (updater.retry_attempts %3C updater.max_retry_
 atte
@@ -1386,34 +1386,34 @@
 _retry_attempts)
-;%0A
+ %7B
 %0A        setTime
@@ -1409,100 +1409,125 @@
     
-setTimeout(function() %7B%0A          // We%22ve tried to reconnect so increment the attempts by 1
+  // Connection has closed so try to reconnect.%0A          updater.retry_attempts++;%0A%0A          updater.socket = null;
 %0A   
@@ -1545,19 +1545,15 @@
 ter.
-attempts ++
+start()
 ;%0A  
@@ -1572,17 +1572,23 @@
 ole.log(
-'
+%22retry_
 attempts
@@ -1589,17 +1589,17 @@
 tempts: 
-'
+%22
 , update
@@ -1600,16 +1600,22 @@
 updater.
+retry_
 attempts
@@ -1630,130 +1630,110 @@
     
-  // Connection has closed so try to reconnect every 10 seconds.%0A          updater.socket = null;%0A          updater.start(
+%7D else %7B%0A          console.log(%22websocket closed by over max_retry_attempts: %22, updater.retry_attempts
 );%0A%0A
@@ -1745,16 +1745,9 @@
    %7D
-, time);
+%0A
 %0A   
@@ -1983,188 +1983,8 @@
   %7D%0A
-  %7D,%0A%0A  generateInterval: function(k) %7B%0A    // generate the interval to a random number between 0 and the max%0A    return Math.min(30, (Math.pow(2, k) - 1)) * 1000 * Math.random();%0A
   %7D%0A

change onclose retry handling