From dbb082f6f6b0feb1c505181f6db86228f7d674ad Mon Sep 17 00:00:00 2001 From: metastasio Date: Thu, 5 Oct 2023 20:38:46 +0300 Subject: [PATCH] Moved socket initialization to init.js --- frontend/src/init.js | 20 ++++++++++++++++++++ frontend/src/socket.js | 36 ++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/frontend/src/init.js b/frontend/src/init.js index dc5cbad..579b2cf 100644 --- a/frontend/src/init.js +++ b/frontend/src/init.js @@ -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'); @@ -42,3 +61,4 @@ const init = async () => { }; export default init; +export { socket, handleEmit }; diff --git a/frontend/src/socket.js b/frontend/src/socket.js index 3d58fe3..f0a70ce 100644 --- a/frontend/src/socket.js +++ b/frontend/src/socket.js @@ -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 };