From e84fd24d3be4943b95b8571c86bf98365ea4c197 Mon Sep 17 00:00:00 2001 From: VictoryJu Date: Tue, 10 Sep 2024 23:49:13 +0900 Subject: [PATCH] [refact]: useSocket refact WIP --- src/App.tsx | 7 +------ src/hooks/socket/useSocket.ts | 29 +++++++++++++++++++++++++- src/pages/Chat/ChatRoom/ChatRoom.tsx | 31 ++++++++++++---------------- src/pages/Loading/LoadingSpinner.tsx | 2 +- 4 files changed, 43 insertions(+), 26 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index c528a93..7f38df5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,6 @@ -import { useEffect } from 'react'; import { RootRouter } from './router/RootRouter'; import { setUserAuth } from './stores/useAuthStore'; -import { showiOSInfo, webviewInit } from './webview/utils'; +import { showiOSInfo } from './webview/utils'; if (!window.handleIosWebviewToken) { window.handleIosWebviewToken = (token, uuid) => { @@ -15,10 +14,6 @@ if (!window.handleIosWebviewToken) { } function App() { - useEffect(() => { - webviewInit(); - }, []); - return ; } diff --git a/src/hooks/socket/useSocket.ts b/src/hooks/socket/useSocket.ts index 9bf933a..62e05eb 100644 --- a/src/hooks/socket/useSocket.ts +++ b/src/hooks/socket/useSocket.ts @@ -2,6 +2,9 @@ import { useAuthStore } from '@/stores/useAuthStore'; import { useEffect, useMemo } from 'react'; import { io } from 'socket.io-client'; +const MAX_ACK_TIME = 10000; +const INITIAL_RETRY = 3; + export const useSocket = (path: 'chat' = 'chat') => { const token = useAuthStore((state) => state.accessToken); @@ -13,16 +16,40 @@ export const useSocket = (path: 'chat' = 'chat') => { extraHeaders: { Authorization: `Bearer ${token}`, }, + ackTimeout: MAX_ACK_TIME, + retries: INITIAL_RETRY, + autoConnect: false, }); }, [token, path]); useEffect(() => { if (!socket) return; + socket.connect(); + return () => { + socket.off('receive'); socket.disconnect(); }; }, [socket]); - return socket; + const sendMessage = (message: object, senderUuid: string) => { + if (socket) { + const socketUniqueId = `${socket.id}-${senderUuid}`; + socket.emit('send', message, socketUniqueId); + } + }; + + const onMessage = (getMessage: (message: string[]) => void) => { + if (socket) socket.on('receive', getMessage); + }; + + interface joinRoomProps { + roomId: string; + } + const joinRoom = ({ roomId }: joinRoomProps) => { + if (socket) socket.emit('join', roomId); + }; + + return { socket, sendMessage, onMessage, joinRoom }; }; diff --git a/src/pages/Chat/ChatRoom/ChatRoom.tsx b/src/pages/Chat/ChatRoom/ChatRoom.tsx index b8eb319..92a5d82 100644 --- a/src/pages/Chat/ChatRoom/ChatRoom.tsx +++ b/src/pages/Chat/ChatRoom/ChatRoom.tsx @@ -22,7 +22,7 @@ const ChatRoom = () => { const { data: previousMessages } = useChatRoomMessages({ chatRoomId, } as ReqChatRoomMessages); - const socket = useSocket(); + const { socket, sendMessage, onMessage, joinRoom } = useSocket(); const [chatMessageType, setChatMessageType] = useState('TEXT'); @@ -30,30 +30,25 @@ const ChatRoom = () => { const [chatMsgs, setChatMsgs] = useState([...previousMessages]); const sendMsg = () => { - if (!socket) return; - socket.emit('send', { - chatRoomId, - chatMessageId, - senderUuid, - chatMessageType, - chatMessageContent, - }); + sendMessage( + { + chatRoomId, + chatMessageId, + senderUuid, + chatMessageType, + chatMessageContent, + }, + senderUuid + ); }; useEffect( function handleChatRoom() { - if (!socket || !chatRoomId) return; - const getMessage = (msg: string[]) => { setChatMsgs([...chatMsgs, ...msg]); }; - - socket.emit('join', { chatRoomId }); - socket.on('receive', getMessage); - - return () => { - socket.off('receive'); - }; + onMessage(getMessage); + joinRoom(chatRoomId); }, [socket, chatRoomId] ); diff --git a/src/pages/Loading/LoadingSpinner.tsx b/src/pages/Loading/LoadingSpinner.tsx index a58f890..a67c0fb 100644 --- a/src/pages/Loading/LoadingSpinner.tsx +++ b/src/pages/Loading/LoadingSpinner.tsx @@ -1,6 +1,6 @@ import Lottie from 'lottie-react'; import spinnerData from './mongleloading.json'; -import styles from './Loading.module.scss'; +import styles from './LoadingSpinner.module.scss'; const LoadingSpinner = () => { return (