Skip to content

Commit 2bc5c68

Browse files
jueunk617namgigun
authored andcommitted
Feat: WebSocket을 통한 실시간 알림 전송 서비스 구현
1 parent d16cee1 commit 2bc5c68

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.back.domain.notification.dto;
2+
3+
import com.back.domain.notification.entity.NotificationType;
4+
5+
import java.time.LocalDateTime;
6+
7+
public record NotificationWebSocketDto(
8+
Long notificationId,
9+
String title,
10+
String message,
11+
NotificationType notificationType,
12+
String targetUrl,
13+
LocalDateTime createdAt
14+
) {
15+
16+
public static NotificationWebSocketDto from(
17+
Long notificationId,
18+
String title,
19+
String content,
20+
NotificationType type,
21+
String targetUrl,
22+
LocalDateTime createdAt) {
23+
24+
return new NotificationWebSocketDto(
25+
notificationId,
26+
title,
27+
content,
28+
type,
29+
targetUrl,
30+
createdAt
31+
);
32+
}
33+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)