From 942c85c9f1ed10612646fd4013b623dd6e7f584d Mon Sep 17 00:00:00 2001 From: Sean Yesmunt Date: Wed, 14 Apr 2021 00:02:32 -0400 Subject: [PATCH] Reconnect when comment websocket loses connection --- ui/redux/actions/websocket.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ui/redux/actions/websocket.js b/ui/redux/actions/websocket.js index ad562ae81..b31149763 100644 --- a/ui/redux/actions/websocket.js +++ b/ui/redux/actions/websocket.js @@ -6,9 +6,10 @@ const NOTIFICATION_WS_URL = 'wss://api.lbry.com/subscribe?auth_token='; const COMMENT_WS_URL = `wss://comments.lbry.com/api/v2/live-chat/subscribe?subscription_id=`; let sockets = {}; +let closingSockets = {}; let retryCount = 0; -export const doSocketConnect = (url, retryOnDisconnect, cb) => { +export const doSocketConnect = (url, cb) => { function connectToSocket() { if (sockets[url] !== undefined && sockets[url] !== null) { sockets[url].close(); @@ -35,11 +36,11 @@ export const doSocketConnect = (url, retryOnDisconnect, cb) => { sockets[url].onclose = () => { console.log('\n Disconnected from WS\n\n'); // eslint-disable-line - if (retryOnDisconnect) { + if (!closingSockets[url]) { retryCount += 1; connectToSocket(); } else { - sockets[url] = null; + closingSockets[url] = null; } }; }, timeToWait); @@ -50,6 +51,7 @@ export const doSocketConnect = (url, retryOnDisconnect, cb) => { export const doSocketDisconnect = (url) => (dispatch) => { if (sockets[url] !== undefined && sockets[url] !== null) { + closingSockets[url] = true; sockets[url].close(); sockets[url] = null; @@ -68,7 +70,7 @@ export const doNotificationSocketConnect = (enableNotifications) => (dispatch) = const url = `${NOTIFICATION_WS_URL}${authToken}`; - doSocketConnect(url, true, (data) => { + doSocketConnect(url, (data) => { switch (data.type) { case 'pending_notifications': if (enableNotifications) { @@ -88,7 +90,7 @@ export const doNotificationSocketConnect = (enableNotifications) => (dispatch) = export const doCommentSocketConnect = (uri, claimId) => (dispatch) => { const url = `${COMMENT_WS_URL}${claimId}`; - doSocketConnect(url, false, (response) => { + doSocketConnect(url, (response) => { if (response.type === 'delta') { const newComment = response.data.comment; dispatch({