๋ฐ์ํ
์ค๋ช
์๋ฒ์์ ํด๋ผ์ด์ธํธ๋ก ๋ฐ์ดํฐ๋ฅผ ์์ ํ๋ ๋ฐฉ๋ฒ (๋จ๋ฐฉํฅ ํต์ )
WebSocket ์ ๊ฒฝ์ฐ, ์๋ฒ <-> ํด๋ผ์ด์ธํธ ๊ฐ ์๋ฐฉํฅ ํต์ ์ ํ์ง๋ง, SSE ์ ๊ฒฝ์ฐ ๋จ๋ฐฉํฅ ํต์ ์ ํจ!
๋ฐ๋ผ์ ๊ฐ๋จํ ์๋ฆผ ์ ์ก ๊ธฐ๋ฅ ๊ฐ๋ฐ๋ฑ์ ์ ํฉํ ๊ธฐ์
๊ตฌํ ์์
// server-side
// case1) SseEmitter ์ด์ฉ
// https://tecoble.techcourse.co.kr/post/2022-10-11-server-sent-events/ ์ฐธ๊ณ
// case2) WebFlux ์ด์ฉ
// ...
@GetMapping(value = "/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> sse() {
return Flux.just("Server time: " + LocalDateTime.now())
.doFinally(signalType -> {
if (signalType == SignalType.CANCEL || signalType == SignalType.ON_COMPLETE) {
log.info("Closing SSE connection");
}
});
..
}
<!-- client-side -->
<!-- server-side ์ case2) ๋ฐฉ์ ๋์ -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SSE TEST</title>
</head>
<body>
<h1>SSE TEST</h1>
<div id="sse-data"></div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
const eventSource = new EventSource("{server-side์ฃผ์}/sse");
eventSource.onmessage = event => {
$("#sse-data").append(event.data); // ์ด๋ถ๋ถ์ ํ ์คํธ ํ์
ํํ๋ก ํํํ๋ฉด ๋จ!
};
eventSource.onerror = error => {
console.error("SSE error:", error);
eventSource.close();
};
</script>
</body>
</html>
๋ฐ์ํ
'๊ฐ๋ฐ > etc' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Vault (1) | 2023.02.07 |
---|---|
ํฅ์ฌ๊ณ ๋ ์ํคํ ์ฒ (Hexagonal Architecture) (2) | 2022.09.25 |
JWT (JSON WEB TOKEN) (0) | 2021.12.22 |
Rest API (0) | 2021.06.02 |
HTTP๊ด๋ จ (https, spdy, ajax, websocket) (0) | 2018.12.09 |