From 7d8a0d03bb392b247c1dd85296fee4a16b9fbc5f Mon Sep 17 00:00:00 2001 From: waterricecake Date: Sun, 10 Nov 2024 17:42:20 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EB=84=A4=EC=9E=84=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20Sse=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EB=A7=A4=EC=84=9C=EB=93=9C=20=EC=9C=84?= =?UTF-8?q?=EC=B9=98=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mafiatogether/common/aspect/SseAspect.java | 12 ------------ .../common/infra/SseEventPublisher.java | 13 +++++++++++-- .../game/application/GameEventListener.java | 2 +- .../mafia/mafiatogether/game/ui/GameController.java | 4 ++-- .../common/infra/SseEventPublisherTest.java | 2 +- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/main/java/mafia/mafiatogether/common/aspect/SseAspect.java b/src/main/java/mafia/mafiatogether/common/aspect/SseAspect.java index 7a3c44c7..6ec70dcd 100644 --- a/src/main/java/mafia/mafiatogether/common/aspect/SseAspect.java +++ b/src/main/java/mafia/mafiatogether/common/aspect/SseAspect.java @@ -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; @@ -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; @@ -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)") @@ -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; - } } diff --git a/src/main/java/mafia/mafiatogether/common/infra/SseEventPublisher.java b/src/main/java/mafia/mafiatogether/common/infra/SseEventPublisher.java index 6922f3a3..b0fb1c38 100644 --- a/src/main/java/mafia/mafiatogether/common/infra/SseEventPublisher.java +++ b/src/main/java/mafia/mafiatogether/common/infra/SseEventPublisher.java @@ -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 emitters = sseEmitterSession.findByCode(code); for (SseEmitter emitter : emitters) { SseEventBuilder builder = getSseEventBuilder(eventName, event); @@ -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; + } } diff --git a/src/main/java/mafia/mafiatogether/game/application/GameEventListener.java b/src/main/java/mafia/mafiatogether/game/application/GameEventListener.java index ef1f232f..cd13edfa 100644 --- a/src/main/java/mafia/mafiatogether/game/application/GameEventListener.java +++ b/src/main/java/mafia/mafiatogether/game/application/GameEventListener.java @@ -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); } } diff --git a/src/main/java/mafia/mafiatogether/game/ui/GameController.java b/src/main/java/mafia/mafiatogether/game/ui/GameController.java index 7bebd800..08722166 100644 --- a/src/main/java/mafia/mafiatogether/game/ui/GameController.java +++ b/src/main/java/mafia/mafiatogether/game/ui/GameController.java @@ -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; @@ -61,7 +61,7 @@ public ResponseEntity 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") diff --git a/src/test/java/mafia/mafiatogether/common/infra/SseEventPublisherTest.java b/src/test/java/mafia/mafiatogether/common/infra/SseEventPublisherTest.java index f0b2dcc4..9bf4ac08 100644 --- a/src/test/java/mafia/mafiatogether/common/infra/SseEventPublisherTest.java +++ b/src/test/java/mafia/mafiatogether/common/infra/SseEventPublisherTest.java @@ -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));