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

经营网站 备案郑州产品设计公司

经营网站 备案,郑州产品设计公司,需要个网站,工程项目立项流程以下是一个完整的基于 Spring Boot 的 Server-Sent Events (SSE) 示例#xff0c;包括服务端和客户端的实现。 一、服务端实现 1. 创建 Spring Boot 项目 首先#xff0c;创建一个基本的 Spring Boot 项目#xff0c;并添加 spring-boot-starter-web 依赖。在 pom.xml 中…以下是一个完整的基于 Spring Boot 的 Server-Sent Events (SSE) 示例包括服务端和客户端的实现。 一、服务端实现 1. 创建 Spring Boot 项目 首先创建一个基本的 Spring Boot 项目并添加 spring-boot-starter-web 依赖。在 pom.xml 中添加以下内容 dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency /dependencies2. 创建 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 langen headmeta charsetUTF-8titleSSE Example/title /head bodyh1Server-Sent Events Example/h1div idevents/divscriptconst 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三、运行和测试 启动 Spring Boot 应用。在浏览器中访问 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 MapString, 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.javaWebFlux 版本 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 FluxServerSentEventString streamSse() {return Flux.interval(Duration.ofSeconds(1)).map(sequence - ServerSentEvent.Stringbuilder().id(String.valueOf(sequence)).event(periodic-event).data(Current time: java.time.LocalTime.now()).build());} }通过以上步骤你可以实现一个完整的基于 Spring Boot 的 SSE 应用。
http://www.w-s-a.com/news/576155/

相关文章:

  • 威海市做网站的做网站很难吗
  • 广州房地产网站建设方案怎么免费申请网站
  • 免费生成网站软件下载影视公司名字取名
  • 网站公司提供程序免费的网页入口
  • jsp网站开发实例教学房产网站怎么做400电话
  • 网络营销方式及流程广州seo工作
  • 专业商城网站制作免费网页设计成品
  • 韩国优秀设计网站找做网站找那个平台做
  • 贵州省清镇市建设学校网站国家企业信用信息公示系统官网河北
  • 游戏界面设计网站网站建设问一问公司
  • 织梦网站模板如何安装教程视频国外哪些网站可以注册域名
  • 用群晖做网站网站中文名称注册
  • 做一个企业网站需要哪些技术app开发公司名字
  • 网站建设有技术的公司图片在线设计平台
  • 建公司网站的详细步骤关于进一步加强网站建设
  • 丰宁县有做网站的吗?维护一个网站一年多少钱
  • 杭州网站设计渠道wordpress购物主题
  • 山东政务网站建设文字logo免费设计在线生成
  • 韩雪个人网站唐山网络运营推广
  • 查建设工程业绩在哪个网站网站建设优化服务如何
  • 江苏省建设工程安全监督网站商洛网站制作
  • 海淀网站建设wzjs51网页设计页面配色分析
  • 网站的备案流程图垦利网站制作
  • 行业用品网站怎么建设外链买东西的网站都有哪些
  • 淘宝做促销的网站集团门户网站建设策划
  • 网站排行榜查询怎样把个人介绍放到百度
  • vps 网站上传河北省招投标信息网
  • 武进网站建设咨询网站定制公司选哪家
  • 郑州市建设投资集团公司网站深圳企业网站建设推荐公司
  • 天津个人网站备案查询dz网站恢复数据库