当前位置: 首页 > news >正文

郑州整站网站优化做网站石材推销

郑州整站网站优化,做网站石材推销,佛山整合营销,wordpress清理不用插件以下是一个完整的基于 Spring Boot 的 Server-Sent Events (SSE) 示例,包括服务端和客户端的实现。 一、服务端实现 1. 创建 Spring Boot 项目 首先,创建一个基本的 Spring Boot 项目,并添加 spring-boot-starter-web 依赖。在 pom.xml 中…

以下是一个完整的基于 Spring Boot 的 Server-Sent Events (SSE) 示例,包括服务端和客户端的实现。

一、服务端实现

1. 创建 Spring Boot 项目

首先,创建一个基本的 Spring Boot 项目,并添加 spring-boot-starter-web 依赖。在 pom.xml 中添加以下内容:

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>
2. 创建 SSE 控制器

创建一个控制器来处理 SSE 连接并推送实时消息。

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. 配置跨域(可选)

如果前端和后端运行在不同端口上,需要配置跨域。

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 {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOriginPatterns("*").allowedMethods("GET", "POST", "PUT", "DELETE").allowedHeaders("*").allowCredentials(true);}
}

二、客户端实现

在前端页面中,使用 EventSource 来订阅 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>

三、运行和测试

  1. 启动 Spring Boot 应用。
  2. 在浏览器中访问 http://localhost:8080,即可看到服务端每秒推送的消息。

四、扩展功能

1. 动态推送消息

可以通过维护一个 SseEmitter 的映射来动态推送消息。

SseController.java(动态推送版本)

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 实现 SSE

如果需要更高效的响应式编程支持,可以使用 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());}
}

通过以上步骤,你可以实现一个完整的基于 Spring Boot 的 SSE 应用。

http://www.yayakq.cn/news/460976/

相关文章:

  • 网站使用的语言网站建设模块有哪些
  • 攻击网站步骤百度竞价一个月5000够吗
  • 制作网页网站费用属于资本性支出吗网站怎么做引流呢
  • 找企业网站制作简书wordpress主题
  • 重庆网络营销网站建设销售义乌网站建设郭
  • 小程序网站开发抖音代运营创业
  • 西北苗木网陕西泽基生态建设有限公司网站网上做预算有哪些网站
  • 企业门户网站的设计与实现公司电商网站开发合同
  • 织梦cms网站模板修改重庆璧山网站制作公司哪家专业
  • 西安的商城网站建设wordpress 9.0apk
  • 网站 特效做cpa怎么建立自己网站
  • 不会写程序如何建网站北京网站建设付款方式
  • 诚信网站平台建设方案.net如何建设网站
  • 网站开发公司业务员培训wordpress如何做一个导航页面
  • 网站首页大图轮播优化推广网站
  • 北京专业网站改版公司wordpress 网站 上传
  • 微网站建设计划书磁力狗在线引擎
  • dw做网站怎么换图片郑州网站建设公司哪家好
  • 建设网站代码西丽网站建设
  • 台州网站建设推广公司网页软件开发
  • 响应式网站推广网站制作最新技术的
  • 化妆品网站模板网络营销的特点主要体现为()
  • 哈尔滨做网站公司社工站建站流程
  • 个人网站设计背景图市场调研方案怎么写
  • 建筑搜索网站东营住房和城乡建设部网站
  • 如何做响应式网站手机访问网站自动跳转
  • 佛山网站建设公司电话如何制作数据库网站
  • 5个在线设计网站微商城小程序定制开发
  • 网站建设的目的模板申请一个电子邮箱
  • 应用公园制作app免费吗wordpress优化网站打开速度