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-04-22 02:17:33 +02:00
|
|
|
const NOTIFICATION_WS_URL = 'wss://sockety.lbry.com/ws/internal?id=';
|
2021-04-20 19:28:22 +02:00
|
|
|
const COMMENT_WS_URL = 'wss://sockety.lbry.com/ws/commentron?id=';
|
2021-03-16 21:59:31 +01:00
|
|
|
|
2021-03-10 19:34:21 +01:00
|
|
|
let sockets = {};
|
2021-04-14 06:02:32 +02:00
|
|
|
let closingSockets = {};
|
2020-09-17 18:39:11 +02:00
|
|
|
let retryCount = 0;
|
|
|
|
|
2021-04-23 22:58:23 +02:00
|
|
|
const getCommentSocketUrl = (claimId) => {
|
|
|
|
return `${COMMENT_WS_URL}${claimId}&category=${claimId}`;
|
|
|
|
};
|
|
|
|
|
2021-04-14 06:02:32 +02: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-04-02 22:11:23 +02:00
|
|
|
|
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
|
2021-04-02 22:11:23 +02:00
|
|
|
// onerror and onclose will both fire, so nothing is needed here
|
2020-09-17 18:39:11 +02:00
|
|
|
};
|
2021-03-16 21:59:31 +01:00
|
|
|
|
|
|
|
sockets[url].onclose = () => {
|
|
|
|
console.log('\n Disconnected from WS\n\n'); // eslint-disable-line
|
2021-04-14 06:02:32 +02:00
|
|
|
if (!closingSockets[url]) {
|
2021-04-02 22:11:23 +02:00
|
|
|
retryCount += 1;
|
|
|
|
connectToSocket();
|
|
|
|
} else {
|
2021-04-14 06:02:32 +02:00
|
|
|
closingSockets[url] = null;
|
2021-04-02 22:11:23 +02:00
|
|
|
}
|
2021-03-16 21:59:31 +01:00
|
|
|
};
|
2020-09-17 18:39:11 +02:00
|
|
|
}, 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-16 21:59:31 +01:00
|
|
|
export const doSocketDisconnect = (url) => (dispatch) => {
|
|
|
|
if (sockets[url] !== undefined && sockets[url] !== null) {
|
2021-04-14 06:02:32 +02:00
|
|
|
closingSockets[url] = true;
|
2021-03-16 21:59:31 +01:00
|
|
|
sockets[url].close();
|
|
|
|
sockets[url] = null;
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.WS_DISCONNECT,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-06 10:05:46 +02:00
|
|
|
export const doNotificationSocketConnect = (enableNotifications) => (dispatch) => {
|
2021-03-10 19:34:21 +01:00
|
|
|
const authToken = getAuthToken();
|
|
|
|
if (!authToken) {
|
|
|
|
console.error('Unable to connect to web socket because auth token is missing'); // eslint-disable-line
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-02 22:11:23 +02:00
|
|
|
const url = `${NOTIFICATION_WS_URL}${authToken}`;
|
|
|
|
|
2021-04-14 06:02:32 +02:00
|
|
|
doSocketConnect(url, (data) => {
|
2021-04-06 10:05:46 +02:00
|
|
|
switch (data.type) {
|
2021-04-19 19:18:47 +02:00
|
|
|
case 'pending_notification':
|
2021-04-06 10:05:46 +02:00
|
|
|
if (enableNotifications) {
|
|
|
|
dispatch(doNotificationList());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'swap-status':
|
2021-04-08 11:02:37 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COIN_SWAP_STATUS_RECEIVED,
|
|
|
|
data: data.data,
|
|
|
|
});
|
2021-04-06 10:05:46 +02:00
|
|
|
break;
|
2021-03-10 19:34:21 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-03-16 19:37:19 +01:00
|
|
|
export const doCommentSocketConnect = (uri, claimId) => (dispatch) => {
|
2021-04-23 22:58:23 +02:00
|
|
|
const url = getCommentSocketUrl(claimId);
|
2021-03-10 19:34:21 +01:00
|
|
|
|
2021-04-14 06:02:32 +02:00
|
|
|
doSocketConnect(url, (response) => {
|
2021-03-10 19:34:21 +01:00
|
|
|
if (response.type === 'delta') {
|
|
|
|
const newComment = response.data.comment;
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.COMMENT_RECEIVED,
|
2021-03-16 19:37:19 +01:00
|
|
|
data: { comment: newComment, claimId, uri },
|
2021-03-10 19:34:21 +01:00
|
|
|
});
|
|
|
|
}
|
2021-06-17 20:55:23 +02:00
|
|
|
if (response.type === 'viewers') {
|
|
|
|
const connected = response.data.connected;
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.VIEWERS_RECEIVED,
|
|
|
|
data: { connected, claimId },
|
|
|
|
});
|
|
|
|
}
|
2021-03-10 19:34:21 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-03-16 21:59:31 +01:00
|
|
|
export const doCommentSocketDisconnect = (claimId) => (dispatch) => {
|
2021-04-23 22:58:23 +02:00
|
|
|
const url = getCommentSocketUrl(claimId);
|
|
|
|
|
2021-03-16 21:59:31 +01:00
|
|
|
dispatch(doSocketDisconnect(url));
|
|
|
|
};
|