Skip to content

Commit 9419b85

Browse files
committed
fix: 워크스페이스 이동 시 기존 소켓 연결 해제
1 parent d6b0f69 commit 9419b85

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

client/src/components/Workspace/index.tsx

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Mom from 'components/Mom';
22
import Sidebar from 'components/Sidebar';
33
import { useEffect, useMemo, useState } from 'react';
44
import { useParams } from 'react-router-dom';
5+
import { Socket } from 'socket.io-client';
56
import { getWorkspaceInfo } from 'src/apis/workspace';
67
import ConfMediaBar from 'src/components/ConfMediaBar';
78
import SOCKET_MESSAGE from 'src/constants/socket-message';
@@ -16,12 +17,12 @@ function Workspace() {
1617
const { id } = useParams();
1718
const [isStart, setIsStart] = useState(false);
1819

19-
const momSocket = useMemo(() => useSocket(`/sc-workspace/${id}`), [id]);
20-
const workspaceSocket = useMemo(() => useSocket(`/workspace/${id}`), [id]);
21-
2220
const [workspace, setWorkspace] = useState<WorkspaceInfo | null>(null);
2321
const [selectedMom, setSelectedMom] = useState<TMom | null>(null);
2422

23+
const [momSocket, setMomSocket] = useState<Socket | null>(null);
24+
const [workspaceSocket, setWorkspaceSocket] = useState<Socket | null>(null);
25+
2526
const loadWorkspaceInfo = async () => {
2627
if (id) {
2728
const workspaceInfo = await getWorkspaceInfo({ id });
@@ -33,18 +34,47 @@ function Workspace() {
3334
};
3435

3536
useEffect(() => {
37+
setMomSocket(prev => {
38+
prev?.disconnect();
39+
return useSocket(`/sc-workspace/${id}`)
40+
});
41+
setWorkspaceSocket(prev => {
42+
prev?.disconnect();
43+
return useSocket(`/workspace/${id}`)
44+
});
45+
46+
loadWorkspaceInfo();
47+
48+
return () => {
49+
setMomSocket(prev => {
50+
prev?.disconnect();
51+
return null;
52+
});
53+
setWorkspaceSocket(prev => {
54+
prev?.disconnect();
55+
return null;
56+
});
57+
}
58+
}, [id]);
59+
60+
useEffect(() => {
61+
if (!workspaceSocket) {
62+
return;
63+
}
64+
3665
workspaceSocket.on(SOCKET_MESSAGE.WORKSPACE.START_MEETING, () => {
3766
setIsStart(true);
3867
});
3968

4069
workspaceSocket.on(SOCKET_MESSAGE.WORKSPACE.END_MEETING, () => {
4170
setIsStart(false);
4271
});
43-
}, []);
4472

45-
useEffect(() => {
46-
loadWorkspaceInfo();
47-
}, [id]);
73+
return () => {
74+
workspaceSocket.off(SOCKET_MESSAGE.WORKSPACE.START_MEETING);
75+
workspaceSocket.off(SOCKET_MESSAGE.WORKSPACE.END_MEETING);
76+
}
77+
}, [workspaceSocket]);
4878

4979
return (
5080
<SocketContext.Provider value={{ momSocket, workspaceSocket }}>

client/src/contexts/socket.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { createContext } from 'react';
22
import { Socket } from 'socket.io-client';
33

44
interface ISocketContext {
5-
momSocket: Socket;
6-
workspaceSocket: Socket;
5+
momSocket: Socket | null;
6+
workspaceSocket: Socket | null;
77
}
88

99
export const SocketContext = createContext<ISocketContext | null>(null);

0 commit comments

Comments
 (0)