From fe37dfecf70cdb3dc3a23bfb9f183ea0a76d93f8 Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Thu, 23 May 2013 11:49:56 -0400 Subject: [PATCH 1/6] connection/disconnection handling --- public/src/app.js | 67 +++++++++++++++++++++++++++++++++---- public/templates/header.tpl | 2 +- 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/public/src/app.js b/public/src/app.js index dbb39ecc45..00abcfa3f0 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -14,6 +14,10 @@ var socket, config = data; socket = io.connect('http://' + config.socket.address + config.socket.port? ':' + config.socket.port : ''); + var reconnecting = false; + var reconnectTries = 0; + + socket.on('event:connect', function(data) { console.log('connected to nodebb socket: ', data); }); @@ -32,14 +36,63 @@ var socket, contentEl.value = data.post; }); - socket.on('disconnect', function(data){ - setTimeout(function() { - $('#disconnect-modal').modal('show'); - $('#reload-button').on('click',function(){ - $('#disconnect-modal').modal('hide'); - window.location.reload(); + socket.on('connect', function(data){ + if(reconnecting) { + app.alert({ + alert_id: 'connection_alert', + title: 'Connected', + message: 'Connection successfull', + type: 'success', + timeout: 5000 }); - }, 500); + reconnecting = false; + } + }); + + socket.on('disconnect', function(data){ + app.alert({ + alert_id: 'connection_alert', + title: 'Disconnect', + message: 'You have disconnected from NodeBB, we will try to reconnect!', + type: 'error', + timeout: 1000 + }); + }); + + socket.on('reconnecting', function(data) { + reconnecting = true; + reconnectTries++; + if(reconnectTries > 4) { + showDisconnectModal(); + return; + } + app.alert({ + alert_id: 'connection_alert', + title: 'Reconnecting', + message: 'You have disconnected from NodeBB, we will try to reconnect you.
', + type: 'notify', + timeout: 5000 + }); + }); + + function showDisconnectModal() { + $('#disconnect-modal').modal({ + backdrop:'static', + show:true + }); + + $('#reload-button').on('click',function(){ + $('#disconnect-modal').modal('hide'); + window.location.reload(); + }); + } + + socket.on('reconnect_failed', function(data) { + console.log('reconnect_failed'); + }); + + socket.on('connect_failed', function(data){ + console.log('connect_faled'); }); }, async: false diff --git a/public/templates/header.tpl b/public/templates/header.tpl index f9746ae08d..ffa9726e56 100644 --- a/public/templates/header.tpl +++ b/public/templates/header.tpl @@ -86,7 +86,7 @@

Socket Disconnect