From b4c42b5241fc2ef1f909cf41fa0c9ab013b6d131 Mon Sep 17 00:00:00 2001 From: dlsxjzld Date: Thu, 4 Apr 2024 21:38:24 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[=F0=9F=90=9Bfix]:=20=EC=9D=B8=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=20=EA=B5=AC=EB=8F=85=20=EC=A4=91=EC=B2=A9=20=EC=95=88?= =?UTF-8?q?=EB=90=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/GamePage/hooks/useWebsocket.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/pages/GamePage/hooks/useWebsocket.ts b/src/pages/GamePage/hooks/useWebsocket.ts index f95424f5..6885ee93 100644 --- a/src/pages/GamePage/hooks/useWebsocket.ts +++ b/src/pages/GamePage/hooks/useWebsocket.ts @@ -1,5 +1,5 @@ import './init.ts'; -import { Client } from '@stomp/stompjs'; +import { Client, StompSubscription } from '@stomp/stompjs'; import { useEffect, useRef } from 'react'; import SockJS from 'sockjs-client'; import { BASE_PATH } from '@/generated/base'; @@ -11,6 +11,7 @@ import { PayloadType } from '../types/websocketType.ts'; const useWebsocket = (roomId: number | null) => { const stompClient = useRef(); + const ingameSubscription = useRef(); const connectHeaders = getToken(); @@ -117,12 +118,18 @@ const useWebsocket = (roomId: number | null) => { }; const onIngameConnected = () => { - stompClient.current?.subscribe( + ingameSubscription.current = stompClient.current?.subscribe( `/from/game/${roomId}`, (e) => onIngameMessageReceived(e), connectHeaders ); }; + const onIngameDisconnected = () => { + if (ingameSubscription.current) { + ingameSubscription.current.unsubscribe(); + } + }; + const handleConnectIngame = (roomId: number) => { stompClient.current?.publish({ destination: `/to/game/${roomId}/connect`, @@ -138,6 +145,7 @@ const useWebsocket = (roomId: number | null) => { setIsIngameWsError(true); } if (responsePublish.type === 'FINISH') { + onIngameDisconnected(); setAllMembers(responsePublish.allMembers); } }; From be17c9f49a8ddbb74df8957e8c63e4677b6b4218 Mon Sep 17 00:00:00 2001 From: dlsxjzld Date: Fri, 5 Apr 2024 16:28:33 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[=E2=99=BB=EF=B8=8Frefactor]:=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=8A=A4=ED=83=80=EC=9D=BC=20=ED=86=B5=EC=9D=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/GamePage/hooks/useWebsocket.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/pages/GamePage/hooks/useWebsocket.ts b/src/pages/GamePage/hooks/useWebsocket.ts index 6885ee93..81a430e1 100644 --- a/src/pages/GamePage/hooks/useWebsocket.ts +++ b/src/pages/GamePage/hooks/useWebsocket.ts @@ -125,9 +125,7 @@ const useWebsocket = (roomId: number | null) => { ); }; const onIngameDisconnected = () => { - if (ingameSubscription.current) { - ingameSubscription.current.unsubscribe(); - } + ingameSubscription.current?.unsubscribe(); }; const handleConnectIngame = (roomId: number) => {