fix websockets not reconnecting
When it gets closed, even for errors, it doesn't always go into Socket.onerror. Add the reconnection logic back to just onclose (like it was before)
This commit is contained in:
parent
84af2dcef0
commit
64928213bf
1 changed files with 14 additions and 7 deletions
|
@ -2,12 +2,13 @@ import * as ACTIONS from 'constants/action_types';
|
||||||
import { getAuthToken } from 'util/saved-passwords';
|
import { getAuthToken } from 'util/saved-passwords';
|
||||||
import { doNotificationList } from 'redux/actions/notifications';
|
import { doNotificationList } from 'redux/actions/notifications';
|
||||||
|
|
||||||
|
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=`;
|
const COMMENT_WS_URL = `wss://comments.lbry.com/api/v2/live-chat/subscribe?subscription_id=`;
|
||||||
|
|
||||||
let sockets = {};
|
let sockets = {};
|
||||||
let retryCount = 0;
|
let retryCount = 0;
|
||||||
|
|
||||||
export const doSocketConnect = (url, cb) => {
|
export const doSocketConnect = (url, retryOnDisconnect, cb) => {
|
||||||
function connectToSocket() {
|
function connectToSocket() {
|
||||||
if (sockets[url] !== undefined && sockets[url] !== null) {
|
if (sockets[url] !== undefined && sockets[url] !== null) {
|
||||||
sockets[url].close();
|
sockets[url].close();
|
||||||
|
@ -21,6 +22,7 @@ export const doSocketConnect = (url, cb) => {
|
||||||
retryCount = 0;
|
retryCount = 0;
|
||||||
console.log('\nConnected to WS \n\n'); // eslint-disable-line
|
console.log('\nConnected to WS \n\n'); // eslint-disable-line
|
||||||
};
|
};
|
||||||
|
|
||||||
sockets[url].onmessage = (e) => {
|
sockets[url].onmessage = (e) => {
|
||||||
const data = JSON.parse(e.data);
|
const data = JSON.parse(e.data);
|
||||||
cb(data);
|
cb(data);
|
||||||
|
@ -28,13 +30,17 @@ export const doSocketConnect = (url, cb) => {
|
||||||
|
|
||||||
sockets[url].onerror = (e) => {
|
sockets[url].onerror = (e) => {
|
||||||
console.error('websocket onerror', e); // eslint-disable-line
|
console.error('websocket onerror', e); // eslint-disable-line
|
||||||
retryCount += 1;
|
// onerror and onclose will both fire, so nothing is needed here
|
||||||
connectToSocket();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
sockets[url].onclose = () => {
|
sockets[url].onclose = () => {
|
||||||
console.log('\n Disconnected from WS\n\n'); // eslint-disable-line
|
console.log('\n Disconnected from WS\n\n'); // eslint-disable-line
|
||||||
|
if (retryOnDisconnect) {
|
||||||
|
retryCount += 1;
|
||||||
|
connectToSocket();
|
||||||
|
} else {
|
||||||
sockets[url] = null;
|
sockets[url] = null;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}, timeToWait);
|
}, timeToWait);
|
||||||
}
|
}
|
||||||
|
@ -60,8 +66,9 @@ export const doNotificationSocketConnect = () => (dispatch) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = `wss://api.lbry.com/subscribe?auth_token=${authToken}`;
|
const url = `${NOTIFICATION_WS_URL}${authToken}`;
|
||||||
doSocketConnect(url, (data) => {
|
|
||||||
|
doSocketConnect(url, true, (data) => {
|
||||||
if (data.type === 'pending_notification') {
|
if (data.type === 'pending_notification') {
|
||||||
dispatch(doNotificationList());
|
dispatch(doNotificationList());
|
||||||
}
|
}
|
||||||
|
@ -71,7 +78,7 @@ export const doNotificationSocketConnect = () => (dispatch) => {
|
||||||
export const doCommentSocketConnect = (uri, claimId) => (dispatch) => {
|
export const doCommentSocketConnect = (uri, claimId) => (dispatch) => {
|
||||||
const url = `${COMMENT_WS_URL}${claimId}`;
|
const url = `${COMMENT_WS_URL}${claimId}`;
|
||||||
|
|
||||||
doSocketConnect(url, (response) => {
|
doSocketConnect(url, false, (response) => {
|
||||||
if (response.type === 'delta') {
|
if (response.type === 'delta') {
|
||||||
const newComment = response.data.comment;
|
const newComment = response.data.comment;
|
||||||
dispatch({
|
dispatch({
|
||||||
|
|
Loading…
Reference in a new issue