From 50585a22b3a454d3ef08c630107d282b776abc87 Mon Sep 17 00:00:00 2001 From: Chrystal Maria Griffiths Date: Fri, 5 Jun 2020 11:15:16 +0100 Subject: [PATCH] Merge pull request #2881 from overleaf/jpa-socket-io-retry-handshake [misc] socket.io: retry the initial connection attempt GitOrigin-RevId: e7f7e8eb233015f41b2b8c67e46c4661b8fad3c9 --- services/web/app/views/project/editor.pug | 1 + services/web/config/settings.defaults.coffee | 1 + .../web/frontend/js/ide/connection/ConnectionManager.js | 8 ++++++++ 3 files changed, 10 insertions(+) diff --git a/services/web/app/views/project/editor.pug b/services/web/app/views/project/editor.pug index 5177c0afe2..81f2b61e08 100644 --- a/services/web/app/views/project/editor.pug +++ b/services/web/app/views/project/editor.pug @@ -179,6 +179,7 @@ block content //- enable doc hash checking for all projects //- used in public/js/libs/sharejs.js window.useShareJsHash = true + window.wsRetryHandshake = #{settings.wsRetryHandshake} - if (settings.overleaf != null) script(type='text/javascript'). diff --git a/services/web/config/settings.defaults.coffee b/services/web/config/settings.defaults.coffee index afa3e907b1..ec954eaec5 100644 --- a/services/web/config/settings.defaults.coffee +++ b/services/web/config/settings.defaults.coffee @@ -211,6 +211,7 @@ module.exports = settings = wsUrlBeta: process.env['WEBSOCKET_URL_BETA'] wsUrlV2Percentage: parseInt(process.env['WEBSOCKET_URL_V2_PERCENTAGE'] || '0', 10) + wsRetryHandshake: parseInt(process.env['WEBSOCKET_RETRY_HANDSHAKE'] || '5', 10) # cookie domain # use full domain for cookies to only be accessible from that domain, diff --git a/services/web/frontend/js/ide/connection/ConnectionManager.js b/services/web/frontend/js/ide/connection/ConnectionManager.js index abe1af2279..afdf2ab597 100644 --- a/services/web/frontend/js/ide/connection/ConnectionManager.js +++ b/services/web/frontend/js/ide/connection/ConnectionManager.js @@ -11,6 +11,7 @@ /* global io */ import SocketIoShim from './SocketIoShim' + let ConnectionManager const ONEHOUR = 1000 * 60 * 60 @@ -136,7 +137,14 @@ export default (ConnectionManager = (function() { // handle network-level websocket errors (e.g. failed dns lookups) + let connectionAttempt = 1 let connectionErrorHandler = err => { + if ( + window.wsRetryHandshake && + connectionAttempt++ < window.wsRetryHandshake + ) { + return setTimeout(() => this.ide.socket.socket.connect(), 100) + } this.updateConnectionManagerState('error') sl_console.log('socket.io error', err) if (this.wsUrl && !window.location.href.match(/ws=fallback/)) {