@@ -10,6 +10,8 @@ import './profile.css';
1010import '../modals/NotificationModal.css' ;
1111import axios from "axios" ;
1212import NotiService from '../services/NotiService' ;
13+ import SockJS from "sockjs-client" ;
14+ import Stomp from "stompjs" ;
1315import webSocketNotificationService from '../services/WebSocketNotificationService' ;
1416
1517const Home = ( ) => {
@@ -34,6 +36,7 @@ const Home = () => {
3436 } ) ;
3537 const [ connected , setConnected ] = useState ( false ) ; // 웹소켓 연결 상태
3638 const navigate = useNavigate ( ) ;
39+ const [ client , setClient ] = useState ( null ) ; // WebSocket 클라이언트 상태
3740
3841
3942 // 사용자 정보 불러오기
@@ -54,7 +57,8 @@ const Home = () => {
5457 } , [ ] ) ;
5558
5659 const toggleModal = async ( ) => {
57- setIsModalOpen ( ! isModalOpen ) ;
60+ // 읽은 알림들을 필터링하여 제거
61+ setNotifications ( prev => prev . filter ( noti => ! noti . read ) ) ;
5862 if ( ! isModalOpen ) { // 모달이 열릴 때 (false -> true)
5963 try {
6064 // 읽지 않은 알림만 필터링
@@ -69,12 +73,14 @@ const Home = () => {
6973 ? { ...noti , read : true }
7074 : noti
7175 ) ) ;
76+
7277 setHasNewNotification ( false ) ; // 모든 알림을 읽음으로 표시했으므로 빨간 점 제거
7378 }
7479 } catch ( error ) {
7580 console . error ( 'Failed to mark notifications as read:' , error ) ;
7681 }
7782 }
83+ setIsModalOpen ( ! isModalOpen ) ;
7884} ;
7985
8086 // 메시지 목록을 로드하는 함수
@@ -98,53 +104,73 @@ const Home = () => {
98104} ;
99105
100106useEffect ( ( ) => {
101- const setupWebSocket = async ( ) => {
102- try {
103- await webSocketNotificationService . connect ( ) ;
104-
105- // 연결 상태 변경 이벤트 리스너 등록
106- const unsubscribeFromConnection = webSocketNotificationService . onConnectionChange ( ( isConnected ) => {
107- console . log ( 'WebSocket connection status:' , isConnected ) ;
108- setConnected ( isConnected ) ;
109-
110- // 연결되면 바로 구독 시도
111- if ( isConnected ) {
112- const success = webSocketNotificationService . subscribeToNotificationChannel ( ) ;
113- console . log ( 'Notification subscription success:' , success ) ;
114- }
115- } ) ;
107+ // userInfo.username이 없으면 WebSocket 연결 안 함
108+ if ( ! userInfo . username ) {
109+ console . log ( "Username is not available yet, waiting..." ) ;
110+ return ;
111+ }
112+
113+ // 초기 알림 데이터 로드
114+ loadNotis ( ) ;
115+
116+ // 기존 WebSocket 연결이 있으면 끊기
117+ if ( client ) {
118+ client . disconnect ( ( ) => {
119+ console . log ( "Previous WebSocket disconnected." ) ;
120+ } ) ;
121+ }
116122
117- // 새로운 알림 메시지 핸들러 등록
118- const unsubscribeFromMessages = webSocketNotificationService . onMessage ( ( newNotification ) => {
119- console . log ( 'New notification received:' , newNotification ) ;
120-
121- // 새로운 알림을 상태에 추가
122- setNotifications ( prev => [ {
123- ...newNotification ,
124- read : false
125- } , ...prev ] ) ;
126-
127- // 새 알림 표시
128- setHasNewNotification ( true ) ;
123+ // 새로운 WebSocket 연결
124+ const socket = new SockJS ( `${ API_BACKEND_URL } /ws` ) ;
125+ const newClient = Stomp . over ( socket ) ;
126+
127+ newClient . connect ( { } , ( ) => {
128+ console . log ( "WebSocket connected!" ) ;
129+
130+ // 커밋 수 업데이트 메시지 수신
131+ newClient . subscribe ( `/topic/notifications/${ userInfo . username } ` , ( message ) => {
132+ const notifications = JSON . parse ( message . body ) ;
133+
134+ // 배열인 경우 처리
135+ if ( Array . isArray ( notifications ) ) {
136+ // 각 알림을 상태에 추가
137+ notifications . forEach ( notification => {
138+ setNotifications ( prev => [ {
139+ id : notification . id ,
140+ message : notification . message ,
141+ createdAt : notification . formattedCreatedAt ,
142+ read : false
143+ } , ...prev ] ) ;
129144 } ) ;
145+ } else {
146+ // 단일 알림인 경우
147+ setNotifications ( prev => [ {
148+ id : notifications . id ,
149+ message : notifications . message ,
150+ createdAt : notifications . formattedCreatedAt ,
151+ read : false
152+ } , ...prev ] ) ;
153+ }
154+
155+ // 새 알림 표시
156+ setHasNewNotification ( true ) ;
157+ } ) ;
158+ } , ( error ) => {
159+ console . error ( "WebSocket error:" , error ) ;
160+ } ) ;
161+
162+ // WebSocket 클라이언트 저장
163+ setClient ( newClient ) ;
130164
131- // 초기 알림 데이터 로드
132- await loadNotis ( ) ;
133-
134- // 컴포넌트 언마운트 시 정리
135- return ( ) => {
136- unsubscribeFromConnection ( ) ;
137- unsubscribeFromMessages ( ) ;
138- webSocketNotificationService . disconnect ( ) ;
139- } ;
140- } catch ( error ) {
141- console . error ( 'Error setting up WebSocket:' , error ) ;
165+ return ( ) => {
166+ if ( newClient ) {
167+ newClient . disconnect ( ( ) => {
168+ console . log ( "WebSocket disconnected." ) ;
169+ } ) ;
142170 }
143- } ;
171+ } ;
172+ } , [ userInfo . username ] ) ; // username이 변경될 때마다 WebSocket 연결
144173
145- setupWebSocket ( ) ;
146- } , [ ] ) ;
147-
148174 useEffect ( ( ) => {
149175 const fetchCommitData = async ( ) => {
150176 try {
0 commit comments