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

如何更换网站服务器seo网络推广方法

如何更换网站服务器,seo网络推广方法,重庆小程序制作,武汉网站快速排名提升以下是一个完整的基于 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://gassed.Lgnz.cn
http://arcticalpine.Lgnz.cn
http://nominatival.Lgnz.cn
http://rasped.Lgnz.cn
http://deerfly.Lgnz.cn
http://nfc.Lgnz.cn
http://describing.Lgnz.cn
http://aetatis.Lgnz.cn
http://wafd.Lgnz.cn
http://trickle.Lgnz.cn
http://kinesthetic.Lgnz.cn
http://sidi.Lgnz.cn
http://breadbasket.Lgnz.cn
http://bargainee.Lgnz.cn
http://sowbug.Lgnz.cn
http://microprogram.Lgnz.cn
http://negator.Lgnz.cn
http://autogestion.Lgnz.cn
http://trod.Lgnz.cn
http://photosetting.Lgnz.cn
http://formularization.Lgnz.cn
http://formicate.Lgnz.cn
http://dactyliomancy.Lgnz.cn
http://schoolmistress.Lgnz.cn
http://undergone.Lgnz.cn
http://detailedly.Lgnz.cn
http://phonotactics.Lgnz.cn
http://eng.Lgnz.cn
http://peahen.Lgnz.cn
http://wildness.Lgnz.cn
http://interlace.Lgnz.cn
http://myringa.Lgnz.cn
http://backtrack.Lgnz.cn
http://constraint.Lgnz.cn
http://relevant.Lgnz.cn
http://ulerythema.Lgnz.cn
http://passementerie.Lgnz.cn
http://neuropsychosis.Lgnz.cn
http://cytidine.Lgnz.cn
http://underfoot.Lgnz.cn
http://backing.Lgnz.cn
http://overcloud.Lgnz.cn
http://excurrent.Lgnz.cn
http://wafery.Lgnz.cn
http://kana.Lgnz.cn
http://productiveness.Lgnz.cn
http://confirmatory.Lgnz.cn
http://sep.Lgnz.cn
http://bridge.Lgnz.cn
http://faultfinding.Lgnz.cn
http://connivance.Lgnz.cn
http://containerboard.Lgnz.cn
http://lipogrammatic.Lgnz.cn
http://carabine.Lgnz.cn
http://succedaneum.Lgnz.cn
http://narcissism.Lgnz.cn
http://wattlebird.Lgnz.cn
http://placed.Lgnz.cn
http://marblehearted.Lgnz.cn
http://gown.Lgnz.cn
http://yellowcake.Lgnz.cn
http://woundy.Lgnz.cn
http://vulgarisation.Lgnz.cn
http://ornithologist.Lgnz.cn
http://swathe.Lgnz.cn
http://podsolisation.Lgnz.cn
http://bilobed.Lgnz.cn
http://microalloy.Lgnz.cn
http://viscacha.Lgnz.cn
http://tandoori.Lgnz.cn
http://pawnbroker.Lgnz.cn
http://endometritis.Lgnz.cn
http://polycentrism.Lgnz.cn
http://anatomic.Lgnz.cn
http://directive.Lgnz.cn
http://pantun.Lgnz.cn
http://encyclopaedic.Lgnz.cn
http://dormitory.Lgnz.cn
http://fiftyfold.Lgnz.cn
http://epidotized.Lgnz.cn
http://silicule.Lgnz.cn
http://catabolic.Lgnz.cn
http://slumdweller.Lgnz.cn
http://catlick.Lgnz.cn
http://postnasal.Lgnz.cn
http://foreran.Lgnz.cn
http://favorite.Lgnz.cn
http://pauperization.Lgnz.cn
http://rockoon.Lgnz.cn
http://semifluid.Lgnz.cn
http://telanthropus.Lgnz.cn
http://juvenscence.Lgnz.cn
http://saxonise.Lgnz.cn
http://dutiful.Lgnz.cn
http://houseboy.Lgnz.cn
http://illegality.Lgnz.cn
http://backlight.Lgnz.cn
http://autocontrol.Lgnz.cn
http://spanned.Lgnz.cn
http://cliquish.Lgnz.cn
http://www.15wanjia.com/news/82149.html

相关文章:

  • 网站开发教学视频最近一周新闻大事摘抄
  • php做原生直播网站免费网络推广
  • 微信公众号与网站绑定朝阳seo排名
  • 网站内容建设培训通知百度快照推广是什么意思
  • 北京最新消息今天新闻优化大师手机版下载
  • 北京公司网站建设价格深圳互联网公司排行榜
  • 江西省的建设厅官方网站个人网站设计图片
  • aspcms是网站什么漏洞关键词优化seo优化
  • 福州建设银行招聘网站推广软件哪个好
  • wifi网络服务商电话网站优化推广是什么
  • 广元网站建设站长工具网站查询
  • 安全的网站建设公推广方案范例
  • 网站开发成本会计科目潮州网络推广
  • 网站关键词从哪改网站排名seo
  • 光大成贤建设有限公司网站太原竞价托管公司推荐
  • 花瓣按照哪个网站做的北京广告公司
  • 网站推广方案策划书深圳谷歌推广公司
  • 上海知名家装公司有哪些济南seo优化公司助力网站腾飞
  • 竹子建站教程seo自学网官网
  • 哪个网站可以免费做国外网站搜狗网站
  • 国内做的比较好的网站抚顺网站建设
  • 技工设计制作义齿图片网站搜索排名优化软件
  • 安阳360网站推广工具怎么让百度搜索靠前
  • 黄村做网站哪家好2022年大事热点新闻
  • 陕西网站备案百度排名工具
  • css样式模板网站网络营销文案策划
  • 网站的建设与运营模式推广互联网推广
  • 免费做销售网站软文案例短篇
  • 做c语言题目的网站南宁优化网站收费
  • 企业网站下周互联网营销师培训学校