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

生猪价格今日猪价行情短视频入口seo

生猪价格今日猪价行情,短视频入口seo,方正宽带网络服务有限公司,如何开拓海外市场SSE(Server-Sent Events)是一种基于HTTP的实时通信协议,它允许服务器向客户端发送持久性的数据流。与WebSocket不同的是,SSE是单向通信,只能由服务器向客户端发送数据。Spring Boot通过Spring WebFlux模块提供了对SSE的…

SSE(Server-Sent Events)是一种基于HTTP的实时通信协议,它允许服务器向客户端发送持久性的数据流。与WebSocket不同的是,SSE是单向通信,只能由服务器向客户端发送数据。Spring Boot通过Spring WebFlux模块提供了对SSE的支持。下面是一个简单的示例:
1、后端

package com.example.springbootmp.controller;import lombok.SneakyThrows;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;import java.io.IOException;
import java.util.Map;
import java.util.concurrent.*;@Controller
@RequestMapping(path = "sse")
@CrossOrigin("*")
public class SseRest {private final static Map<String, SseEmitter> sseCache = new ConcurrentHashMap<>();/*** 连接sse服务,并向前端推送数据* @param id* @return* @throws IOException*/@GetMapping(path = "subscribe", produces = {MediaType.TEXT_EVENT_STREAM_VALUE})@SneakyThrowspublic SseEmitter push(String id) throws IOException {// 超时时间设置为5分钟,用于演示客户端自动重连SseEmitter sseEmitter = new SseEmitter(5_60_000L);// 设置前端的重试时间为1ssseCache.put(id, sseEmitter);ExecutorService executorService= Executors.newFixedThreadPool(1,(Runnable r)->{Thread t=new Thread(r);t.setDaemon(true);return t;});SseEmitter.SseEventBuilder data = SseEmitter.event().name("message").id(id).data("测试数据");executorService.execute(()->{while (true){if(sseCache.containsKey(id)){System.out.println("发送");try {sseEmitter.send(data);Thread.sleep(2000);} catch (IOException e) {e.printStackTrace();}catch (InterruptedException e) {e.printStackTrace();}}else {System.out.println("结束");break;}}});//        while (true){
//            sseEmitter.send("测试数据",MediaType.APPLICATION_JSON);
//            Thread.sleep(1000);
//        }// onCompletion(): 结束之后的回调触发//sseEmitter.onCompletion(() -> System.out.println("完成!!!"));return sseEmitter;}/*** http://127.0.0.1:8080/sse/push?id=7777&content=%E4%BD%A0%E5%93%88aaaaaa* @param id* @param content* @return* @throws IOException*/@ResponseBody@GetMapping(path = "push")public String push(String id, String content) throws IOException {SseEmitter sseEmitter = sseCache.get(id);if (sseEmitter != null) {sseEmitter.send(content);}return "over";}@ResponseBody@GetMapping(path = "/over/{id}")public String over(@PathVariable("id") String id) {SseEmitter sseEmitter = sseCache.get(id);if (sseEmitter != null) {// complete(): 表示执行完毕,会断开连接sseEmitter.complete();sseCache.remove(id);}return "over";}
}

2、前端

var source
//开始建立连接部分
source = new EventSource('http://localhost:9999/sse/subscribe?id=122')source.addEventListener('message',function(event) {console.log('接收数据')console.log(event.data)},false)
//关闭连接部分
source.close()
http://www.ds6.com.cn/news/65048.html

相关文章:

  • 苏州网站设计电话软文推广案例大全
  • 盐城网站建设哪家快线上推广宣传方式有哪些
  • 丹灶网站建设公司安徽网络seo
  • 温州软件开发站长之家的seo综合查询工具
  • 个人备案网站盈利企业seo推广
  • 大庆做网站的公司神马快速排名优化工具
  • 设计素材网站排版品牌推广方案策划书
  • 网站建设开发工具百度正式员工工资待遇
  • WordPress 列表如何修改成图片seo是什么意思seo是什么职位
  • 广东企业网站模板设计如何建立自己的网页
  • 办办网官网营销型网站建设优化建站
  • 做音乐网站怎么放音乐seo优化方案项目策划书
  • 新闻资讯网站怎么做专业软文发稿平台
  • 惠州网站建设学校恶意点击竞价时用的什么软件
  • 合肥市住房和城乡建设局百度seo关键词排名s
  • 官方网站建设与维护好处seo推广排名平台有哪些
  • 永康网站建设百度地图轨迹导航
  • 哪个网站专门做政策解读seo 优化案例
  • wx5 做网站可以么新app推广方案
  • python 网站开发框架什么是整合营销概念
  • 做网站工作内容seo哪家强
  • 高端网站建设的方案网站流量监控
  • ae有么有做gif的网站青岛百度关键词优化
  • 什么网站可以做期刊封面深圳网络推广团队
  • 网站微信链接怎么做的什么是seo站内优化
  • 做网站跟app需要多少钱黄页网络的推广网站有哪些软件
  • 做网站 工资高吗关键词seo公司
  • 网站加关键词代码网上怎么推广公司产品
  • 郑州网站开发工程师推广软文范例100字
  • 建https网站360提交入口网址