Spring Boot實(shí)現(xiàn)SSE實(shí)時(shí)推送實(shí)戰(zhàn)示例
以下是一個(gè)完整的基于 Spring Boot 的 Server-Sent Events (SSE) 示例,包括服務(wù)端和客戶(hù)端的實(shí)現(xiàn)。
一、服務(wù)端實(shí)現(xiàn)
1. 創(chuàng)建 Spring Boot 項(xiàng)目
首先,創(chuàng)建一個(gè)基本的 Spring Boot 項(xiàng)目,并添加 spring-boot-starter-web 依賴(lài)。在 pom.xml 中添加以下內(nèi)容:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>2. 創(chuàng)建 SSE 控制器
創(chuàng)建一個(gè)控制器來(lái)處理 SSE 連接并推送實(shí)時(shí)消息。
SseController.java
package com.example.sse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@RestController
public class SseController {
private final ExecutorService executorService = Executors.newCachedThreadPool();
@GetMapping("/sse")
public SseEmitter handleSse() {
SseEmitter emitter = new SseEmitter();
executorService.execute(() -> {
try {
for (int i = 0; i < 10; i++) {
emitter.send("Message " + i, MediaType.TEXT_PLAIN);
TimeUnit.SECONDS.sleep(1);
}
emitter.complete();
} catch (IOException | InterruptedException e) {
emitter.completeWithError(e);
}
});
return emitter;
}
}3. 配置跨域(可選)
如果前端和后端運(yùn)行在不同端口上,需要配置跨域。
CorsConfig.java
package com.example.sse;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("*")
.allowCredentials(true);
}
}二、客戶(hù)端實(shí)現(xiàn)
在前端頁(yè)面中,使用 EventSource 來(lái)訂閱 SSE。
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SSE Example</title>
</head>
<body>
<h1>Server-Sent Events Example</h1>
<div id="events"></div>
<script>
const eventSource = new EventSource('/sse');
eventSource.onmessage = function(event) {
const newElement = document.createElement("div");
newElement.innerHTML = "Message: " + event.data;
document.getElementById("events").appendChild(newElement);
};
eventSource.onerror = function(event) {
eventSource.close();
alert("EventSource failed: " + event);
};
</script>
</body>
</html>三、運(yùn)行和測(cè)試
- 啟動(dòng) Spring Boot 應(yīng)用。
- 在瀏覽器中訪(fǎng)問(wèn)
http://localhost:8080,即可看到服務(wù)端每秒推送的消息。
四、擴(kuò)展功能
1. 動(dòng)態(tài)推送消息
可以通過(guò)維護(hù)一個(gè) SseEmitter 的映射來(lái)動(dòng)態(tài)推送消息。
SseController.java(動(dòng)態(tài)推送版本)
package com.example.sse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@RestController
public class SseController {
private final Map<String, SseEmitter> emitterMap = new ConcurrentHashMap<>();
@GetMapping("/sse/{userId}")
public SseEmitter connect(@PathVariable String userId) {
SseEmitter emitter = new SseEmitter();
emitterMap.put(userId, emitter);
emitter.onCompletion(() -> emitterMap.remove(userId));
emitter.onTimeout(() -> emitterMap.remove(userId));
emitter.onError(e -> emitterMap.remove(userId));
return emitter;
}
@GetMapping("/push/{userId}")
public void push(@PathVariable String userId, @RequestParam String message) {
SseEmitter emitter = emitterMap.get(userId);
if (emitter != null) {
try {
emitter.send(message);
} catch (IOException e) {
emitter.completeWithError(e);
emitterMap.remove(userId);
}
}
}
}2. 使用 WebFlux 實(shí)現(xiàn) SSE
如果需要更高效的響應(yīng)式編程支持,可以使用 Spring WebFlux。
SseController.java(WebFlux 版本)
package com.example.sse;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import java.time.Duration;
@RestController
public class SseController {
@GetMapping("/sse/stream")
public Flux<ServerSentEvent<String>> streamSse() {
return Flux.interval(Duration.ofSeconds(1))
.map(sequence -> ServerSentEvent.<String>builder()
.id(String.valueOf(sequence))
.event("periodic-event")
.data("Current time: " + java.time.LocalTime.now())
.build());
}
}通過(guò)以上步驟,你可以實(shí)現(xiàn)一個(gè)完整的基于 Spring Boot 的 SSE 應(yīng)用。
到此這篇關(guān)于Spring Boot實(shí)現(xiàn)SSE實(shí)時(shí)推送實(shí)戰(zhàn)示例的文章就介紹到這了,更多相關(guān)springboot sse實(shí)時(shí)推送內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用遞歸刪除樹(shù)形結(jié)構(gòu)的所有子節(jié)點(diǎn)(java和mysql實(shí)現(xiàn))
下面小編就為大家?guī)?lái)一篇使用遞歸刪除樹(shù)形結(jié)構(gòu)的所有子節(jié)點(diǎn)(java和mysql實(shí)現(xiàn))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Feign?日期格式轉(zhuǎn)換錯(cuò)誤的問(wèn)題
這篇文章主要介紹了Feign?日期格式轉(zhuǎn)換錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
解決Spring Cloud Gateway獲取body內(nèi)容,不影響GET請(qǐng)求的操作
這篇文章主要介紹了解決Spring Cloud Gateway獲取body內(nèi)容,不影響GET請(qǐng)求的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
Java實(shí)戰(zhàn)之實(shí)現(xiàn)在線(xiàn)小說(shuō)閱讀系統(tǒng)
本文主要介紹了一個(gè)通過(guò)Java實(shí)現(xiàn)的在線(xiàn)電子書(shū)小說(shuō)閱讀系統(tǒng),文中用到的技術(shù)有Layui、Springboot、SpringMVC、HTML、FTP、JavaScript、JQuery等,感興趣的可以試試2022-01-01
Java多線(xiàn)程窗口售票問(wèn)題實(shí)例
這篇文章主要介紹了Java多線(xiàn)程窗口售票問(wèn)題實(shí)例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
Java處理UnresolvedAddressException異常的問(wèn)題及解決
這篇文章主要介紹了Java處理UnresolvedAddressException異常的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-05-05

