Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[be] SseEmitter를 Body에 리턴하게끔 수정 #156

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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