Skip to content

Commit 97c23cb

Browse files
committed
[FE] refactor: 중복 구독 방지 및 token 로직 개선 (#74)
1 parent 69453b6 commit 97c23cb

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

src/frontend/src/pages/ChannelPage/hooks/useStompWebRTC.ts

+37-19
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,57 @@ interface UseStompWebRTCProps {
88
const useStompWebRTC = ({ roomId }: UseStompWebRTCProps) => {
99
const [isConnected, setIsConnected] = useState(false);
1010
const clientRef = useRef<Client | null>(null);
11+
const connectionAttempts = useRef(0);
1112

1213
const SERVER_URL = import.meta.env.VITE_SIGNALING;
1314

1415
useEffect(() => {
16+
if (!roomId) return;
17+
1518
const token = localStorage.getItem('access_token');
16-
let socketToken: string;
17-
if (token) socketToken = token;
19+
if (!token) return;
20+
21+
if (clientRef.current) {
22+
console.log('이전 STOMP 연결 정리 중...');
23+
clientRef.current.deactivate();
24+
clientRef.current = null;
25+
}
26+
27+
connectionAttempts.current = 0;
1828

1929
const client = new Client({
20-
webSocketFactory: () => new WebSocket(SERVER_URL, ['v10.stomp', socketToken]),
30+
webSocketFactory: () => new WebSocket(SERVER_URL, ['v10.stomp', token]),
2131
connectHeaders: { Authorization: `Bearer ${token}` },
22-
// debug: (msg) => console.log('STOMP DEBUG:', msg),
32+
debug: (msg) => console.log('STOMP DEBUG:', msg),
2333
reconnectDelay: 5000,
24-
heartbeatIncoming: 10000,
25-
heartbeatOutgoing: 10000,
34+
heartbeatIncoming: 4000,
35+
heartbeatOutgoing: 4000,
2636

2737
onConnect: (frame: Frame) => {
2838
console.log('✅ STOMP 연결 성공!', frame);
39+
connectionAttempts.current = 0;
40+
setIsConnected(true);
2941

30-
// 연결 성공 시 subscribe
31-
client.subscribe(`/topic/users/${roomId}`, (message) => {
32-
console.log('📩 users 받은 메시지:', message);
33-
});
34-
35-
client.subscribe(`/topic/answer/${roomId}`, (message) => {
36-
console.log('📩 answer 받은 메시지:', message);
37-
});
38-
39-
client.subscribe(`/topic/candidate/${roomId}`, (message) => {
40-
console.log('📩 candidate 받은 메시지:', message);
41-
});
42+
const subscriptions = [];
4243

43-
setIsConnected(true);
44+
// 연결 성공 시 subscribe
45+
subscriptions.push(
46+
client.subscribe(`/topic/users/${roomId}`, (message) => {
47+
console.log('📩 users 받은 메시지:', message);
48+
}),
49+
);
50+
51+
subscriptions.push(
52+
client.subscribe(`/topic/answer/${roomId}`, (message) => {
53+
console.log('📩 answer 받은 메시지:', message);
54+
}),
55+
);
56+
57+
subscriptions.push(
58+
client.subscribe(`/topic/candidate/${roomId}`, (message) => {
59+
console.log('📩 candidate 받은 메시지:', message);
60+
}),
61+
);
4462
},
4563

4664
onWebSocketError: (error: Error) => {

0 commit comments

Comments
 (0)