Skip to content

Commit

Permalink
feat: SSE DTO 분리 및 프론트 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
lass9436 committed Aug 24, 2024
1 parent b7f8ffb commit fb037c5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion react/src/components/PerformanceSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function PerformanceSelect() {
if (data.status === "SELECTED" || data.status === "SELECTABLE") {
setSeats(prevSeats => {
return prevSeats.map(seat =>
String(seat.seatId) === data.seatId
seat.seatId === data.seatId
? { ...seat, seatAvailable: data.status === "SELECTABLE" }
: seat
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.thirdparty.ticketing.domain.common.LoginMember;
import com.thirdparty.ticketing.domain.ticket.dto.sse.SeatEventResponse;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class TicketSseController {

private final ConcurrentMap<Long, ConcurrentMap<String, SseEmitter>> emitters =
new ConcurrentHashMap<>();

private final ObjectMapper objectMapper;

@GetMapping("/subscribe/performances/{performanceId}")
public SseEmitter subscribePerformances(
@LoginMember String memberEmail, @PathVariable("performanceId") Long performanceId) {
Expand Down Expand Up @@ -79,28 +85,21 @@ public void releaseSeat(

@Async
public void sendEventToPerformanceAsync(
String memberEmail, Long performanceId, String eventName, Long data) {
String memberEmail, Long performanceId, String eventName, Long seatId) {
log.debug("공연 ID: {}, 이벤트: {}에 대한 비동기 브로드캐스트를 시작합니다", performanceId, eventName);
ConcurrentMap<String, SseEmitter> performanceEmitters = emitters.get(performanceId);
if (performanceEmitters != null) {
String status = eventName.equals("SELECT") ? "SELECTED" : "SELECTABLE";
SeatEventResponse eventData = new SeatEventResponse(seatId, status);
performanceEmitters.forEach(
(email, emitter) -> {
// 특정 이메일로는 이벤트를 전송하지 않음
if (email.equals(memberEmail)) {
log.debug("공연 ID: {}, 이메일: {}로는 이벤트를 전송하지 않음", performanceId, email);
return;
}
try {
emitter.send(
SseEmitter.event()
.name(eventName)
.data(
"{\"seatId\":\""
+ data
+ "\",\"status\":\""
+ status
+ "\"}"));
String jsonData = objectMapper.writeValueAsString(eventData);
emitter.send(SseEmitter.event().name(eventName).data(jsonData));
log.debug(
"공연 ID: {}, 이미터 ID: {}로 이벤트가 전송되었습니다. 이벤트: {}, 상태: {}",
performanceId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.thirdparty.ticketing.domain.ticket.dto.sse;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class SeatEventResponse {
private Long seatId;
private String status;
}

0 comments on commit fb037c5

Please sign in to comment.