@@ -8,39 +8,57 @@ interface UseStompWebRTCProps {
8
8
const useStompWebRTC = ( { roomId } : UseStompWebRTCProps ) => {
9
9
const [ isConnected , setIsConnected ] = useState ( false ) ;
10
10
const clientRef = useRef < Client | null > ( null ) ;
11
+ const connectionAttempts = useRef ( 0 ) ;
11
12
12
13
const SERVER_URL = import . meta. env . VITE_SIGNALING ;
13
14
14
15
useEffect ( ( ) => {
16
+ if ( ! roomId ) return ;
17
+
15
18
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 ;
18
28
19
29
const client = new Client ( {
20
- webSocketFactory : ( ) => new WebSocket ( SERVER_URL , [ 'v10.stomp' , socketToken ] ) ,
30
+ webSocketFactory : ( ) => new WebSocket ( SERVER_URL , [ 'v10.stomp' , token ] ) ,
21
31
connectHeaders : { Authorization : `Bearer ${ token } ` } ,
22
- // debug: (msg) => console.log('STOMP DEBUG:', msg),
32
+ debug : ( msg ) => console . log ( 'STOMP DEBUG:' , msg ) ,
23
33
reconnectDelay : 5000 ,
24
- heartbeatIncoming : 10000 ,
25
- heartbeatOutgoing : 10000 ,
34
+ heartbeatIncoming : 4000 ,
35
+ heartbeatOutgoing : 4000 ,
26
36
27
37
onConnect : ( frame : Frame ) => {
28
38
console . log ( '✅ STOMP 연결 성공!' , frame ) ;
39
+ connectionAttempts . current = 0 ;
40
+ setIsConnected ( true ) ;
29
41
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 = [ ] ;
42
43
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
+ ) ;
44
62
} ,
45
63
46
64
onWebSocketError : ( error : Error ) => {
0 commit comments