From deb2084fc5da975b1d571f26412bb351c39a42d6 Mon Sep 17 00:00:00 2001 From: Yann Flory Date: Mon, 7 Mar 2016 12:00:45 +0100 Subject: [PATCH] Add the socket connection checker --- www/common/netflux.js | 35 +++++++++---------------- www/common/realtime-input.js | 51 ++++++++++++++++++------------------ 2 files changed, 39 insertions(+), 47 deletions(-) diff --git a/www/common/netflux.js b/www/common/netflux.js index 1d0e9c809..6919a72cd 100644 --- a/www/common/netflux.js +++ b/www/common/netflux.js @@ -1025,12 +1025,6 @@ return /******/ (function(modules) { // webpackBootstrap this.protocol = _ServiceProvider2.default.get(cs.EXCHANGEPROTOCOL_SERVICE); this.defaults = { signaling: 'ws://localhost:9000', - /** - * If an error is encountered but it is recoverable, do not immediately fail - * but if it keeps firing errors over and over, do fail. - */ - recoverableErrorCount: 0, - MAX_RECOVERABLE_ERRORS: 15, // Maximum number of milliseconds of lag before we fail the connection. MAX_LAG_BEFORE_DISCONNECT: 20000 }; @@ -1040,6 +1034,8 @@ return /******/ (function(modules) { // webpackBootstrap _createClass(WebSocketService, [{ key: 'join', value: function join(key) { + var _this = this; + var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; var settings = Object.assign({}, this.settings, options); @@ -1051,24 +1047,19 @@ return /******/ (function(modules) { // webpackBootstrap resolve(socket); }; socket.onerror = reject; - }); - } - - // Check the status of the socket connection - /*var isSocketDisconnected = function (realtime) { - let sock = ws._socket; - return sock.readyState === sock.CLOSING - || sock.readyState === sock.CLOSED - || (realtime.getLag().waiting && realtime.getLag().lag > MAX_LAG_BEFORE_DISCONNECT); - } - var checkSocket = module.exports.checkSocket = function (realtime) { - if (isSocketDisconnected(realtime) && !socket.intentionallyClosing) { + // Check the status of the socket connection + var isSocketDisconnected = function isSocketDisconnected(realtime, sock) { + return sock.readyState === sock.CLOSING || sock.readyState === sock.CLOSED || realtime.getLag().waiting && realtime.getLag().lag > _this.settings.MAX_LAG_BEFORE_DISCONNECT; + }; + socket.checkSocket = function (realtime) { + if (isSocketDisconnected(realtime, socket) && !socket.intentionallyClosing) { return true; - } else { + } else { return false; - } - };*/ - + } + }; + }); + } }]); return WebSocketService; diff --git a/www/common/realtime-input.js b/www/common/realtime-input.js index ecd19e015..08473e3f8 100644 --- a/www/common/realtime-input.js +++ b/www/common/realtime-input.js @@ -31,7 +31,7 @@ define([ var debug = function (x) { console.log(x); }, warn = function (x) { console.error(x); }, verbose = function (x) { console.log(x); }; - // verbose = function () {}; // comment out to enable verbose logging + verbose = function () {}; // comment out to enable verbose logging // ------------------ Trapping Keyboard Events ---------------------- // @@ -130,7 +130,7 @@ define([ }); // Check the connection to the channel - //checkConnection(wc); + checkConnection(wc); bindAllEvents(textarea, doc, onEvent, false); @@ -202,31 +202,32 @@ define([ } var checkConnection = function(wc) { - //TODO - /*var socketChecker = setInterval(function () { - if (netflux.checkSocket(realtime)) { - warn("Socket disconnected!"); - - recoverableErrorCount += 1; - - if (recoverableErrorCount >= MAX_RECOVERABLE_ERRORS) { - warn("Giving up!"); - realtime.abort(); - wc.leave() - .then(null, function(err) { - warn(err); - }); - if (config.onAbort) { - config.onAbort({ - socket: socket - }); + if(wc.channels && wc.channels.size > 0) { + var channels = Array.from(wc.channels); + var channel = channels[0]; + + var socketChecker = setInterval(function () { + if (channel.checkSocket(realtime)) { + warn("Socket disconnected!"); + + recoverableErrorCount += 1; + + if (recoverableErrorCount >= MAX_RECOVERABLE_ERRORS) { + warn("Giving up!"); + realtime.abort(); + try { channel.close(); } catch (e) { warn(e); } + if (config.onAbort) { + config.onAbort({ + socket: channel + }); + } + if (socketChecker) { clearInterval(socketChecker); } } - if (socketChecker) { clearInterval(socketChecker); } + } else { + // it's working as expected, continue } - } else { - // it's working as expected, continue - } - }, 200);*/ + }, 200); + } } return {