Skip to content

Commit

Permalink
refactor : 메서드 네임 수정 및 Sse 생성 매서드 위치 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
waterricecake committed Nov 10, 2024
1 parent c908a37 commit 7d8a0d0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 18 deletions.
12 changes: 0 additions & 12 deletions src/main/java/mafia/mafiatogether/common/aspect/SseAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import lombok.RequiredArgsConstructor;
import mafia.mafiatogether.common.annotation.PlayerInfo;
import mafia.mafiatogether.common.infra.SseEventPublisher;
import mafia.mafiatogether.common.exception.AuthException;
import mafia.mafiatogether.common.exception.ExceptionCode;
import mafia.mafiatogether.common.resolver.PlayerInfoDto;
Expand All @@ -13,10 +12,7 @@
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter.SseEventBuilder;


import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
Expand All @@ -26,7 +22,6 @@
@RequiredArgsConstructor
public class SseAspect {

public static final long HOURS_12 = 43200_000L;
private final SseEmitterSession sseEmitterSession;

@Around("@annotation(mafia.mafiatogether.common.annotation.SseSubscribe)")
Expand Down Expand Up @@ -65,11 +60,4 @@ public Object subscribe(final ProceedingJoinPoint joinPoint) throws Throwable {
private boolean hasPlayerInfo(final Annotation[] annotations) {
return Arrays.stream(annotations).anyMatch(PlayerInfo.class::isInstance);
}

public static SseEmitter getSseEmitter(final String name, final Object event) throws IOException {
final SseEmitter sseEmitter = new SseEmitter(HOURS_12);
SseEventBuilder sseEventBuilder = SseEventPublisher.getSseEventBuilder(name, event);
sseEmitter.send(sseEventBuilder);
return sseEmitter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
@RequiredArgsConstructor
public class SseEventPublisher {

public static final long SECOND_30 = 30_000L;
private static final long HOURS_12 = 43200_000L;
private static final long SECOND_30 = 30_000L;
private final SseEmitterSession sseEmitterSession;

public void publishEventToAllSseClient(final String code, final String eventName, final Object event) {
public void publishEventByCode(final String code, final String eventName, final Object event) {
List<SseEmitter> emitters = sseEmitterSession.findByCode(code);
for (SseEmitter emitter : emitters) {
SseEventBuilder builder = getSseEventBuilder(eventName, event);
Expand Down Expand Up @@ -53,4 +54,12 @@ public void publishEventByCodeAndName(
public void disconnectSseByCode(final String code) {
sseEmitterSession.deleteByCode(code);
}


public static SseEmitter getSseEmitter(final String name, final Object event) throws IOException {
final SseEmitter sseEmitter = new SseEmitter(HOURS_12);
SseEventBuilder sseEventBuilder = SseEventPublisher.getSseEventBuilder(name, event);
sseEmitter.send(sseEventBuilder);
return sseEmitter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,6 @@ public void listenGameStatusChangeEvent(final GameStatusChangeEvent gameStatusCh

private void sendStatusChangeEventToSseClient(final String code, final StatusType statusType) {
final GameStatusResponse gameStatusResponse = new GameStatusResponse(statusType);
sseEventPublisher.publishEventToAllSseClient(code, GAME_STATUS_EVENT_NAME, gameStatusResponse);
sseEventPublisher.publishEventByCode(code, GAME_STATUS_EVENT_NAME, gameStatusResponse);
}
}
4 changes: 2 additions & 2 deletions src/main/java/mafia/mafiatogether/game/ui/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import lombok.RequiredArgsConstructor;
import mafia.mafiatogether.common.annotation.PlayerInfo;
import mafia.mafiatogether.common.aspect.SseAspect;
import mafia.mafiatogether.common.infra.SseEventPublisher;
import mafia.mafiatogether.common.resolver.PlayerInfoDto;
import mafia.mafiatogether.common.annotation.SseSubscribe;
import mafia.mafiatogether.game.application.GameService;
Expand Down Expand Up @@ -61,7 +61,7 @@ public ResponseEntity<GameInfoResponse> findGameInfo(
@GetMapping(path = "/subscribe", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter subscribe(@PlayerInfo final PlayerInfoDto playerInfoDto) throws IOException {
GameStatusResponse gameStatusResponse = gameService.findStatus(playerInfoDto.code());
return SseAspect.getSseEmitter(SSE_STATUS, gameStatusResponse);
return SseEventPublisher.getSseEmitter(SSE_STATUS, gameStatusResponse);
}

@GetMapping("/valid")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void setSseEmitters() {
String event = "event";

// when
sseEventPublisher.publishEventToAllSseClient(CODE, EVENT_NAME, event);
sseEventPublisher.publishEventByCode(CODE, EVENT_NAME, event);

// then
verify(SSE_EMITTER_1).send(any(SseEmitter.SseEventBuilder.class));
Expand Down

0 comments on commit 7d8a0d0

Please sign in to comment.