Skip to content

Commit 6900a6e

Browse files
author
EpicFn
committed
feat : 하트비트 로직 구현
1 parent c8991ff commit 6900a6e

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/main/java/org/tuna/zoopzoop/backend/BackendApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
6+
import org.springframework.scheduling.annotation.EnableScheduling;
67

78
@SpringBootApplication
89
@EnableJpaAuditing
10+
@EnableScheduling
911
public class BackendApplication {
1012
public static void main(String[] args) {
1113
SpringApplication.run(BackendApplication.class, args);

src/main/java/org/tuna/zoopzoop/backend/domain/SSE/service/EmitterService.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.tuna.zoopzoop.backend.domain.SSE.service;
22

3+
import org.springframework.scheduling.annotation.Scheduled;
34
import org.springframework.stereotype.Service;
45
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
56

@@ -53,4 +54,22 @@ public void sendNotification(Long memberId, String eventName, Object data) {
5354
}
5455
}
5556
}
57+
58+
/**
59+
* 20초마다 모든 Emitter에 하트비트 전송
60+
* 클라이언트와의 연결 유지를 위해 주기적으로 빈 이벤트를 전송
61+
*/
62+
@Scheduled(fixedRate = 20000)
63+
public void sendHeartbeat() {
64+
// 모든 Emitter에 하트비트 전송
65+
emitters.forEach((userId, emitter) -> {
66+
try {
67+
// SSE 주석(comment)을 사용하여 클라이언트에서 별도 이벤트를 발생시키지 않음
68+
emitter.send(SseEmitter.event().comment("keep-alive"));
69+
} catch (IOException e) {
70+
// 전송 실패 시, 클라이언트 연결이 끊어진 것으로 간주하고 Map에서 제거
71+
emitters.remove(userId);
72+
}
73+
});
74+
}
5675
}

0 commit comments

Comments
 (0)