From 5099357a8ac740a7fe53c8637cdcf42fcc5b1f32 Mon Sep 17 00:00:00 2001 From: Tor Hveem Date: Fri, 27 Mar 2015 10:22:42 +0100 Subject: [PATCH] Better disconnect responsiveness Instead of waiting for the websocket to really close, which can take a long time because network latency, weechat inresponsiveness etc, we just set our status to disconnected when user wants to disconnect, and we let the websocket handle the close in its own time in the background. If the user wants to reconnect this means there will be a new websocket connection before the old one has failed, but this works just fine. --- js/connection.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/js/connection.js b/js/connection.js index 70c0065..9c5f649 100644 --- a/js/connection.js +++ b/js/connection.js @@ -130,17 +130,14 @@ weechat.factory('connection', }; var handleClose = function (evt) { - ngWebsockets.failCallbacks('disconnection'); - $rootScope.connected = false; - $rootScope.$emit('relayDisconnect'); if (ssl && evt && evt.code === 1006) { // A password error doesn't trigger onerror, but certificate issues do. Check time of last error. if (typeof $rootScope.lastError !== "undefined" && (Date.now() - $rootScope.lastError) < 1000) { // abnormal disconnect by client, most likely ssl error $rootScope.sslError = true; + $rootScope.$apply(); } } - $rootScope.$apply(); }; var onerror = function (evt) { @@ -235,8 +232,19 @@ weechat.factory('connection', }; var disconnect = function() { + $log.info('Disconnecting from relay'); $rootScope.userdisconnect = true; ngWebsockets.send(weeChat.Protocol.formatQuit()); + // In case the backend doesn't repond we will close from our end + var closeTimer = setTimeout(function() { + ngWebsockets.disconnect(); + // We pretend we are not connected anymore + // The connection can time out on its own + ngWebsockets.failCallbacks('disconnection'); + $rootScope.connected = false; + $rootScope.$emit('relayDisconnect'); + $rootScope.$apply(); + }); }; /*