lbry-desktop/ui/redux/actions/websocket.js

56 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

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';
let socket = null;
let retryCount = 0;
2020-07-23 16:22:57 +02:00
export const doSocketConnect = () => dispatch => {
const authToken = getAuthToken();
if (!authToken) {
console.error('Unable to connect to web socket because auth token is missing'); // eslint-disable-line
return;
}
function connectToSocket() {
if (socket !== null) {
socket.close();
socket = null;
2020-07-23 16:22:57 +02:00
}
const timeToWait = retryCount ** 2 * 1000;
setTimeout(() => {
const url = `wss://api.lbry.com/subscribe?auth_token=${authToken}`;
socket = new WebSocket(url);
socket.onopen = e => {
retryCount = 0;
console.log('\nConnected to WS \n\n'); // eslint-disable-line
};
socket.onmessage = e => {
const data = JSON.parse(e.data);
if (data.type === 'pending_notification') {
dispatch(doNotificationList());
}
};
socket.onerror = e => {
2020-12-08 22:50:10 +01:00
console.error('websocket onerror', e); // eslint-disable-line
// onerror and onclose will both fire, so nothing is needed here
};
socket.onclose = e => {
2020-12-08 22:50:10 +01:00
console.error('websocket onclose', e); // eslint-disable-line
retryCount += 1;
connectToSocket();
};
}, timeToWait);
}
2020-07-23 16:22:57 +02:00
connectToSocket();
2020-07-23 16:22:57 +02:00
};
export const doSocketDisconnect = () => ({
type: ACTIONS.WS_DISCONNECT,
});