Skip to content

Commit

Permalink
Moved socket initialization to init.js
Browse files Browse the repository at this point in the history
  • Loading branch information
metastasio committed Oct 5, 2023
1 parent b503252 commit dbb082f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
20 changes: 20 additions & 0 deletions frontend/src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,30 @@ import i18next from 'i18next';
import { I18nextProvider, initReactI18next } from 'react-i18next';
import * as leoProfanity from 'leo-profanity';
import { Provider as RollbarProvider, ErrorBoundary } from '@rollbar/react';
import { io } from 'socket.io-client';

import store from './store';
import resources from './services/locales/index.js';
import App from './App.js';

const URL = process.env.NODE_ENV === 'production' ? undefined : 'http://localhost:5001';
const socket = io(URL, {
autoConnect: false,
});

const handleEmit = async (event, payload, onError, onSuccess) => {
try {
const response = await socket.emitWithAck(event, payload);
if (response) {
if (onSuccess && typeof onSuccess === 'function') {
onSuccess(response.data);
}
}
} catch (err) {
onError();
}
};

const init = async () => {
const i18n = i18next.createInstance();
const russianDictionary = leoProfanity.getDictionary('ru');
Expand Down Expand Up @@ -42,3 +61,4 @@ const init = async () => {
};

export default init;
export { socket, handleEmit };
36 changes: 18 additions & 18 deletions frontend/src/socket.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { io } from 'socket.io-client';
// import { io } from 'socket.io-client';

const URL = process.env.NODE_ENV === 'production' ? undefined : 'http://localhost:5001';
// const URL = process.env.NODE_ENV === 'production' ? undefined : 'http://localhost:5001';

const socket = io(URL, {
autoConnect: false,
});
// const socket = io(URL, {
// autoConnect: false,
// });

const handleEmit = async (event, payload, onError, onSuccess) => {
try {
const response = await socket.emitWithAck(event, payload);
if (response) {
if (onSuccess && typeof onSuccess === 'function') {
onSuccess(response.data);
}
}
} catch (err) {
onError();
}
};
// const handleEmit = async (event, payload, onError, onSuccess) => {
// try {
// const response = await socket.emitWithAck(event, payload);
// if (response) {
// if (onSuccess && typeof onSuccess === 'function') {
// onSuccess(response.data);
// }
// }
// } catch (err) {
// onError();
// }
// };

export { socket, handleEmit };
// export { socket, handleEmit };

0 comments on commit dbb082f

Please sign in to comment.