@@ -29,6 +29,9 @@ const MESSAGE_PAGE_SIZE = 20;
2929// 읽음 처리 딜레이
3030const READ_PROCESSING_DELAY_MS = 500 ;
3131
32+ // ping 간격 (45초)
33+ const PRESENCE_PING_INTERVAL_MS = 45000 ;
34+
3235interface ChatSendRequest {
3336 chatRoomId : number ;
3437 messageType : "TEXT" | "IMAGE" ;
@@ -371,6 +374,8 @@ export function useChatRoomSocket(
371374 useEffect ( ( ) => {
372375 if ( ! chatRoomId || ! accessToken ) return ;
373376
377+ let pingInterval : NodeJS . Timeout | null = null ;
378+
374379 const client = new Client ( {
375380 webSocketFactory : ( ) => new SockJS ( `${ API_URL } ${ API_ENDPOINTS . wsStomp } ` ) ,
376381 connectHeaders : { Authorization : `Bearer ${ accessToken } ` } ,
@@ -379,6 +384,21 @@ export function useChatRoomSocket(
379384 client . subscribe ( API_ENDPOINTS . wsChatRoom ( chatRoomId ) , handleMessageReceived ) ;
380385 client . subscribe ( API_ENDPOINTS . wsRealTimeRead ( chatRoomId ) , handleReadEventReceived ) ;
381386 client . subscribe ( API_ENDPOINTS . wsUserQueueErrors , handleQueueError ) ;
387+
388+ // 채팅방 진입 ws
389+ client . publish ( {
390+ destination : API_ENDPOINTS . wsEnterChatRoom ,
391+ body : JSON . stringify ( { chatRoomId : Number ( chatRoomId ) } ) ,
392+ } ) ;
393+
394+ // ping 체크 (45초)
395+ pingInterval = setInterval ( ( ) => {
396+ if ( client . connected ) {
397+ client . publish ( {
398+ destination : API_ENDPOINTS . wsCheckPing ,
399+ } ) ;
400+ }
401+ } , PRESENCE_PING_INTERVAL_MS ) ;
382402 } ,
383403 onStompError : handleStompError ,
384404 } ) ;
@@ -387,6 +407,19 @@ export function useChatRoomSocket(
387407 clientRef . current = client ;
388408
389409 return ( ) => {
410+ // ping 제거
411+ if ( pingInterval ) {
412+ clearInterval ( pingInterval ) ;
413+ }
414+
415+ // 이탈 ws
416+ if ( client . connected ) {
417+ client . publish ( {
418+ destination : API_ENDPOINTS . wsLeaveChatRoom ,
419+ body : JSON . stringify ( { chatRoomId : Number ( chatRoomId ) } ) ,
420+ } ) ;
421+ }
422+
390423 client . deactivate ( ) ;
391424 clientRef . current = null ;
392425 } ;
0 commit comments