1. nakamura
  2. tai

Commits

nakamura  committed 591da33

chat.js: increase reconnection timeout

  • Participants
  • Parent commits 432762f
  • Branches default

Comments (0)

Files changed (1)

File tai/service/static/js/chat.js

View file
         onclose: function() {},
         onmessage: function() {},
 
+        initialReconnectTimeout: 5 * 1000,
+        reconnectTimeout: null,
+
         start: function() {
             self.socket = new SockJS(config.sockURL);
 
             self.socket.onopen = function() {
+                console.log('Connected.');
+                self.reconnectTimeout = null;
+
                 var message = {};
                 message[config.sessionKeyName] = getCookie(config.sessionKeyName);
                 self.socket.send(JSON.stringify(message));
             }
 
             self.socket.onclose = function() {
-                console.log('Server connection closed. Reconnecting ...');
+                if (!self.reconnectTimeout) {
+                    self.reconnectTimeout = self.initialReconnectTimeout;
+                } else {
+                    self.reconnectTimeout = _.min([self.reconnectTimeout * 2, 60 * 1000]);
+                }
+
+                console.log('Server connection closed. Reconnecting in ' + self.reconnectTimeout / 1000 + ' seconds ...');
                 if (self.onclose) {
                     self.onclose();
                 }
 
-                window.setTimeout(self.start, 10000);
+                window.setTimeout(self.start, self.reconnectTimeout);
             }
 
             self.socket.onmessage = function(event) {