1+ package com .back .domain .notification .service ;
2+
3+ import com .back .domain .notification .dto .NotificationWebSocketDto ;
4+ import lombok .RequiredArgsConstructor ;
5+ import lombok .extern .slf4j .Slf4j ;
6+ import org .springframework .messaging .simp .SimpMessagingTemplate ;
7+ import org .springframework .stereotype .Service ;
8+
9+ @ Slf4j
10+ @ Service
11+ @ RequiredArgsConstructor
12+ public class NotificationWebSocketService { // WebSocket을 통한 실시간 알림 전송 서비스
13+
14+ private final SimpMessagingTemplate messagingTemplate ;
15+
16+ // 특정 유저에게 알림 전송
17+ public void sendNotificationToUser (Long userId , NotificationWebSocketDto notificationDto ) {
18+ try {
19+ String destination = "/topic/user/" + userId + "/notifications" ;
20+ messagingTemplate .convertAndSend (destination , notificationDto );
21+
22+ log .info ("실시간 알림 전송 성공 - 유저 ID: {}, 알림 ID: {}, 제목: {}" ,
23+ userId , notificationDto .notificationId (), notificationDto .title ());
24+
25+ } catch (Exception e ) {
26+ log .error ("실시간 알림 전송 실패 - 유저 ID: {}, 오류: {}" , userId , e .getMessage (), e );
27+ }
28+ }
29+
30+ // 전체 유저에게 시스템 알림 브로드캐스트
31+ public void broadcastSystemNotification (NotificationWebSocketDto notificationDto ) {
32+ try {
33+ String destination = "/topic/notifications/system" ;
34+ messagingTemplate .convertAndSend (destination , notificationDto );
35+
36+ log .info ("시스템 알림 브로드캐스트 성공 - 알림 ID: {}, 제목: {}" ,
37+ notificationDto .notificationId (), notificationDto .title ());
38+
39+ } catch (Exception e ) {
40+ log .error ("시스템 알림 브로드캐스트 실패 - 오류: {}" , e .getMessage (), e );
41+ }
42+ }
43+
44+ // 스터디룸 멤버들에게 알림 전송
45+ public void sendNotificationToRoom (Long roomId , NotificationWebSocketDto notificationDto ) {
46+ try {
47+ String destination = "/topic/room/" + roomId + "/notifications" ;
48+ messagingTemplate .convertAndSend (destination , notificationDto );
49+
50+ log .info ("스터디룸 알림 전송 성공 - 룸 ID: {}, 알림 ID: {}, 제목: {}" ,
51+ roomId , notificationDto .notificationId (), notificationDto .title ());
52+
53+ } catch (Exception e ) {
54+ log .error ("스터디룸 알림 전송 실패 - 룸 ID: {}, 오류: {}" , roomId , e .getMessage (), e );
55+ }
56+ }
57+ }
0 commit comments