From 3891b180bee5b6168e0e7f2016a50236a2d1359f Mon Sep 17 00:00:00 2001 From: Sully <2dcoder@naver.com> Date: Thu, 5 Oct 2023 11:40:45 +0900 Subject: [PATCH] =?UTF-8?q?fix=20#155:=20SseEmitter=EB=A5=BC=20Body?= =?UTF-8?q?=EC=97=90=20=EB=A6=AC=ED=84=B4=ED=95=98=EA=B2=8C=EB=81=94=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../carrot/notification/component/SseEmitters.java | 8 +++++--- .../notification/controller/NotificationController.java | 9 +++++---- .../carrot/notification/service/NotificationService.java | 5 +++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/be/carrot/src/main/java/com/example/carrot/notification/component/SseEmitters.java b/be/carrot/src/main/java/com/example/carrot/notification/component/SseEmitters.java index ee9124aff..44011473c 100644 --- a/be/carrot/src/main/java/com/example/carrot/notification/component/SseEmitters.java +++ b/be/carrot/src/main/java/com/example/carrot/notification/component/SseEmitters.java @@ -28,18 +28,18 @@ public class SseEmitters { @Value("${sse.timeout}") private long emitterTimeout; - public void add(Long userId) { + public SseEmitter add(Long userId) { SseEmitter emitter = new SseEmitter(emitterTimeout); userSseEmitterMap.put(userId, emitter); - sendNotification(userId, EVENT_NAME_CONNECTED); + return sendNotification(userId, EVENT_NAME_CONNECTED); } public void sendNotification(Long receiverId, Notification notification) { sendNotification(receiverId, notification.toString()); } - private void sendNotification(Long receiverId, String notificationData) { + private SseEmitter sendNotification(Long receiverId, String notificationData) { SseEmitter emitter = userSseEmitterMap.get(receiverId); try { @@ -47,6 +47,8 @@ private void sendNotification(Long receiverId, String notificationData) { .id(String.valueOf(receiverId)) .name(EVENT_NAME_NOTIFICATION) .data(notificationData)); + + return emitter; } catch (IOException e) { emitter.completeWithError(new CustomException(StatusCode.SEND_SSE_EMITTER_ERROR)); } diff --git a/be/carrot/src/main/java/com/example/carrot/notification/controller/NotificationController.java b/be/carrot/src/main/java/com/example/carrot/notification/controller/NotificationController.java index 46a2ff44a..f16d9094a 100644 --- a/be/carrot/src/main/java/com/example/carrot/notification/controller/NotificationController.java +++ b/be/carrot/src/main/java/com/example/carrot/notification/controller/NotificationController.java @@ -1,11 +1,14 @@ package com.example.carrot.notification.controller; +import javax.servlet.http.HttpServletResponse; + import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import com.example.carrot.notification.service.NotificationService; @@ -19,11 +22,9 @@ public class NotificationController { private final NotificationService notificationService; @GetMapping(value = "/subscribe", produces = MediaType.TEXT_EVENT_STREAM_VALUE) - public ResponseEntity connect(@RequestAttribute Long userId) { - notificationService.subscribe(userId); - + public ResponseEntity connect(@RequestAttribute Long userId) { return ResponseEntity.ok() .header("X-Accel-Buffering", "no") - .build(); + .body(notificationService.subscribe(userId)); } } diff --git a/be/carrot/src/main/java/com/example/carrot/notification/service/NotificationService.java b/be/carrot/src/main/java/com/example/carrot/notification/service/NotificationService.java index 982572c38..79592eb4b 100644 --- a/be/carrot/src/main/java/com/example/carrot/notification/service/NotificationService.java +++ b/be/carrot/src/main/java/com/example/carrot/notification/service/NotificationService.java @@ -1,6 +1,7 @@ package com.example.carrot.notification.service; import org.springframework.stereotype.Service; +import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import com.example.carrot.notification.component.SseEmitters; import com.example.carrot.notification.entity.Notification; @@ -16,8 +17,8 @@ public class NotificationService { private final UserRepository userRepository; private final SseEmitters sseEmitters; - public void subscribe(Long userId) { - sseEmitters.add(userId); + public SseEmitter subscribe(Long userId) { + return sseEmitters.add(userId); } public void send(User receiver, Notification notification) {