From 8f428f15a37ded2c04d25519aef0ce3a9ffe297d Mon Sep 17 00:00:00 2001 From: ntr Date: Wed, 6 Nov 2019 19:05:28 +1100 Subject: [PATCH] expo connect backoff and reloading --- client/src/socket.jsx | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/client/src/socket.jsx b/client/src/socket.jsx index b89bc187..fbc6b296 100644 --- a/client/src/socket.jsx +++ b/client/src/socket.jsx @@ -318,8 +318,11 @@ function createSocket(events) { return handlers[msgType](params); } + let attempts = 1; + // Connection opened function onOpen() { + attempts = 0; toast.info({ message: 'connected', position: 'topRight', @@ -338,12 +341,21 @@ function createSocket(events) { } function onClose(event) { + attempts *= 2; + if (attempts > 10) { + toast.warning({ + message: 'unable to connect, refreshing...', + position: 'topRight', + }); + setTimeout(() => window.location.reload(true), 2000); + } + console.error('WebSocket closed', event); toast.warning({ message: 'disconnected', position: 'topRight', }); - return setTimeout(connect, 5000); + return setTimeout(connect, attempts * 1000); } function connect() {