2022-06-19 08:16:35 -05:00
|
|
|
import { Socket, io } from 'socket.io-client';
|
|
|
|
|
|
2022-07-01 12:00:12 -05:00
|
|
|
let websocket: Socket;
|
|
|
|
|
|
2022-07-26 12:28:07 -05:00
|
|
|
export const openWebsocketConnection = () => {
|
2022-06-24 04:18:50 +01:00
|
|
|
try {
|
2022-07-26 12:28:07 -05:00
|
|
|
websocket = io('', {
|
2022-06-24 04:18:50 +01:00
|
|
|
path: '/api/socket.io',
|
|
|
|
|
transports: ['polling'],
|
|
|
|
|
reconnection: true,
|
|
|
|
|
forceNew: true,
|
2022-07-26 12:28:07 -05:00
|
|
|
autoConnect: true
|
2022-06-24 04:18:50 +01:00
|
|
|
});
|
2022-06-19 08:16:35 -05:00
|
|
|
|
2022-06-24 04:18:50 +01:00
|
|
|
listenToEvent(websocket);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log('Cannot connect to websocket ', e);
|
|
|
|
|
}
|
2022-06-19 08:16:35 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const listenToEvent = (socket: Socket) => {
|
2022-09-08 17:30:49 +02:00
|
|
|
//TODO: if we are not using this, we should probably remove it?
|
|
|
|
|
socket.on('on_upload_success', () => undefined);
|
2022-06-19 08:16:35 -05:00
|
|
|
|
|
|
|
|
socket.on('error', (e) => {
|
|
|
|
|
console.log('Websocket Error', e);
|
|
|
|
|
});
|
|
|
|
|
};
|
2022-07-01 12:00:12 -05:00
|
|
|
|
|
|
|
|
export const closeWebsocketConnection = () => {
|
|
|
|
|
websocket?.close();
|
|
|
|
|
};
|