Skip to content

Commit

Permalink
Merge pull request #27 from lotteon2/feat/welcome-message
Browse files Browse the repository at this point in the history
Feat/welcome message - 1st merge
  • Loading branch information
wakkpu authored Jan 14, 2024
2 parents 1f0f3ff + 7bed232 commit b9ac8c8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.dailyon.notificationservice.domain.notification.document.NotificationTemplate;
import com.dailyon.notificationservice.domain.notification.dto.HeartbeatServerSentEvent;
import com.dailyon.notificationservice.domain.notification.dto.NotificationData;
import com.dailyon.notificationservice.domain.notification.dto.WelcomeServerSentEvent;
import com.dailyon.notificationservice.domain.notification.service.NotificationService;
import com.dailyon.notificationservice.domain.notification.service.SseNotificationService;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -99,6 +100,8 @@ public Mono<String> createOrUpdateRestockNotification(
@GetMapping(value = "/subscription", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ServerSentEvent<NotificationData>> subscribeToNotifications(@RequestHeader Long memberId) {
log.info("SSE 연결 시작" + "memberId: " + memberId);

Flux<ServerSentEvent<NotificationData>> welcomeFlux = Flux.just(WelcomeServerSentEvent.getInstance());

Flux<ServerSentEvent<NotificationData>> notificationFlux = sseNotificationService.streamNotifications(memberId);
log.info("Notification stream Flux 생성됨 - memberId: {}", memberId);
Expand All @@ -107,7 +110,7 @@ public Flux<ServerSentEvent<NotificationData>> subscribeToNotifications(@Request
.map(tick -> HeartbeatServerSentEvent.getInstance())
.doOnNext(tick -> log.info("Hearbeat event sent - memberId: {}", memberId)); // 반복적 송신 객체 싱글톤으로 처리

return Flux.merge(notificationFlux, heartbeatFlux)
return Flux.concat(welcomeFlux, Flux.merge(notificationFlux, heartbeatFlux))
.doOnError(e -> log.error("Error in SSE stream for member {}: {}", memberId, e.getMessage(), e))
.doOnTerminate(() -> log.info("SSE stream for member {} 종료", memberId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public enum NotificationType {
AUCTION_END("실시간 경매 종료", "실시간 경매가 종료되었습니다."),
GIFT_RECEIVED("선물", "선물을 받았습니다."),
POINTS_EARNED_SNS("SNS 구매유도 포인트 적립", "SNS를 통해 포인트가 적립되었습니다."),
HEARTBEAT("하트비트", "연결 유지용 주기적 송신.");
HEARTBEAT("하트비트", "연결 유지용 주기적 송신."),
WELCOME("웰컴메세지", "최초 접속 메세지");
// 정의하면서 넣을 예정

private final String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.dailyon.notificationservice.domain.notification.dto;

import com.dailyon.notificationservice.domain.notification.document.enums.NotificationType;
import org.springframework.http.codec.ServerSentEvent;

public class WelcomeServerSentEvent {

private static final NotificationData WELCOME_INSTANCE = NotificationData.builder()
.id(null)
.message(null)
.linkUrl(null)
.notificationType(NotificationType.WELCOME)
.read(false)
.build();

private static final ServerSentEvent<NotificationData> WELCOME_EVENT = ServerSentEvent.<NotificationData>builder()
.data(WELCOME_INSTANCE)
.build();

public static ServerSentEvent<NotificationData> getInstance() {
return WELCOME_EVENT;
}
}

0 comments on commit b9ac8c8

Please sign in to comment.