Skip to content
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 @@ -136,14 +136,16 @@ public void sendMessageToUser(String userId, String message) {

public void sendAll(SendAlarm sendAlarm) {
sseEmitters.forEach((userId, emitter) -> {
System.out.println("sendAll userId = " + userId);
System.out.println("Attempting to send to userId = " + userId);
try {
emitter.send(
SseEmitter.event()
.name(String.valueOf(sendAlarm.getStatus()))
.data(sendAlarm, MediaType.APPLICATION_JSON)
);
SseEmitter.SseEventBuilder event = SseEmitter.event()
.name(String.valueOf(sendAlarm.getStatus()))
.data(sendAlarm, MediaType.APPLICATION_JSON);
System.out.println("Event data: " + sendAlarm);
emitter.send(event);
System.out.println("Successfully sent to userId = " + userId);
} catch (IOException e) {
System.err.println("Error sending to userId = " + userId + ": " + e.getMessage());
sseEmitters.remove(userId);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -37,7 +38,8 @@ public class AlarmController implements AlarmApi {

private final AlarmService alarmService;

@GetMapping(value = "/subscribe", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@CrossOrigin(origins = "*", allowedHeaders = "*")
@GetMapping("/subscribe")
@PreAuthorize("hasRole('ROLE_USER')")
public SseEmitter subscribe(@AuthenticationPrincipal CustomUserDetails user) {
return alarmService.subscribe(user.getId());
Expand All @@ -64,6 +66,7 @@ public void send(
alarmService.newAlarmRequest(alarmId);
}

@CrossOrigin(origins = "*", allowedHeaders = "*")
@GetMapping
@PreAuthorize("hasRole('ROLE_USER')")
public BaseResponse<PageResponse<AlarmResponse>> list(
Expand Down