2020-07-23 16:22:57 +02:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
|
|
|
import { getAuthToken } from 'util/saved-passwords';
|
|
|
|
import { doNotificationList } from 'redux/actions/notifications';
|
|
|
|
|
2021-03-10 19:34:21 +01:00
|
|
|
let sockets = {};
|
2020-09-17 18:39:11 +02:00
|
|
|
let retryCount = 0;
|
|
|
|
|
2021-03-10 19:34:21 +01:00
|
|
|
export const doSocketConnect = (url, cb) => {
|
2020-09-17 18:39:11 +02:00
|
|
|
function connectToSocket() {
|
2021-03-10 19:34:21 +01:00
|
|
|
if (sockets[url] !== undefined && sockets[url] !== null) {
|
|
|
|
sockets[url].close();
|
|
|
|
sockets[url] = null;
|
2020-07-23 16:22:57 +02:00
|
|
|
}
|
|
|
|
|
2020-09-17 18:39:11 +02:00
|
|
|
const timeToWait = retryCount ** 2 * 1000;
|
|
|
|
setTimeout(() => {
|
2021-03-10 19:34:21 +01:00
|
|
|
sockets[url] = new WebSocket(url);
|
|
|
|
sockets[url].onopen = (e) => {
|
2020-09-17 18:39:11 +02:00
|
|
|
retryCount = 0;
|
|
|
|
console.log('\nConnected to WS \n\n'); // eslint-disable-line
|
|
|
|
};
|
2021-03-10 19:34:21 +01:00
|
|
|
sockets[url].onmessage = (e) => {
|
2020-09-17 18:39:11 +02:00
|
|
|
const data = JSON.parse(e.data);
|
2021-03-10 19:34:21 +01:00
|
|
|
cb(data);
|
2020-09-17 18:39:11 +02:00
|
|
|
};
|
|
|
|
|
2021-03-10 19:34:21 +01:00
|
|
|
sockets[url].onerror = (e) => {
|
2020-12-08 22:50:10 +01:00
|
|
|
console.error('websocket onerror', e); // eslint-disable-line
|
2020-09-17 18:39:11 +02:00
|
|
|
// onerror and onclose will both fire, so nothing is needed here
|
|
|
|
};
|
|
|
|
|
2021-03-10 19:34:21 +01:00
|
|
|
sockets[url].onclose = (e) => {
|
2020-12-08 22:50:10 +01:00
|
|
|
console.error('websocket onclose', e); // eslint-disable-line
|
2020-09-17 18:39:11 +02:00
|
|
|
retryCount += 1;
|
|
|
|
connectToSocket();
|
|
|
|
};
|
|
|
|
}, timeToWait);
|
|
|
|
}
|
2020-07-23 16:22:57 +02:00
|
|
|
|
2020-09-17 18:39:11 +02:00
|
|
|
connectToSocket();
|
2020-07-23 16:22:57 +02:00
|
|
|
};
|
|
|
|
|
2021-03-10 19:34:21 +01:00
|
|
|
export const doNotificationSocketConnect = () => (dispatch) => {
|
|
|
|
const authToken = getAuthToken();
|
|
|
|
if (!authToken) {
|
|
|
|
console.error('Unable to connect to web socket because auth token is missing'); // eslint-disable-line
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = `wss://api.lbry.com/subscribe?auth_token=${authToken}`;
|
|
|
|
doSocketConnect(url, (data) => {
|
|
|
|
if (data.type === 'pending_notification') {
|
|
|
|
dispatch(doNotificationList());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const doCommentSocketConnect = (claimId) => (dispatch) => {
|
|
|
|
const url = `wss://comments.lbry.com/api/v2/live-chat/subscribe?subscription_id=${claimId}`;
|
|
|
|
|
|
|
|
doSocketConnect(url, (response) => {
|
|
|
|
if (response.type === 'delta') {
|
|
|
|
const newComment = response.data.comment;
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_RECEIVED,
|
|
|
|
data: { comment: newComment, claimId },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-07-23 16:22:57 +02:00
|
|
|
export const doSocketDisconnect = () => ({
|
|
|
|
type: ACTIONS.WS_DISCONNECT,
|
|
|
|
});
|