Skip to content

Commit

Permalink
Merge pull request #156 from masters2023-4th-project-carrot-talk/fix/#…
Browse files Browse the repository at this point in the history
…155

[be] SseEmitter를 Body에 리턴하게끔 수정
  • Loading branch information
devbattery authored Oct 5, 2023
2 parents 43c170d + 3891b18 commit d64bee1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,27 @@ 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 {
emitter.send(SseEmitter.event()
.id(String.valueOf(receiverId))
.name(EVENT_NAME_NOTIFICATION)
.data(notificationData));

return emitter;
} catch (IOException e) {
emitter.completeWithError(new CustomException(StatusCode.SEND_SSE_EMITTER_ERROR));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -19,11 +22,9 @@ public class NotificationController {
private final NotificationService notificationService;

@GetMapping(value = "/subscribe", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public ResponseEntity<Void> connect(@RequestAttribute Long userId) {
notificationService.subscribe(userId);

public ResponseEntity<SseEmitter> connect(@RequestAttribute Long userId) {
return ResponseEntity.ok()
.header("X-Accel-Buffering", "no")
.build();
.body(notificationService.subscribe(userId));
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit d64bee1

Please sign in to comment.