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

淄博营销网站建设公司合肥做网站推广

淄博营销网站建设公司,合肥做网站推广,网站如何开启gzip压缩,郑州网站营销推广公司什么是sse? 1、SSE 是Server-Sent Events(服务器发送事件) 2、SSE是一种允许服务器主动向客户端推送实时更新的技术。 3、它基于HTTP协议,并使用了其长连接特性,在客户端与服务器之间建立一条持久化的连接。 通过这条连接&am…

什么是sse?

1、SSE 是Server-Sent Events(服务器发送事件)

2、SSE是一种允许服务器主动向客户端推送实时更新的技术。

3、它基于HTTP协议,并使用了其长连接特性,在客户端与服务器之间建立一条持久化的连接。 通过这条连接,服务器可以实时地向客户端发送事件流,而客户端可以监听这些事件并作出相应的处理。

4、SSE是单向通信机制,即只能由服务器向客户端推送数据,客户端不能通过SSE向服务器发送数据。

5、SSE在现代浏览器和移动设备上得到了广泛的支持,是实现实时Web应用的一种有效方式。

使用流程(经测试,此方式不会丢失消息,靠谱能用!

1、引入springboot的web基本依赖,这里不细说

2、controller中

 /*** 订阅sse消息** @return*/@CrossOrigin@RequestMapping(path = "/subscribe/{userId}")public SseEmitter subscribe(@PathVariable String userId) {// 设置超时时间,0表示不过期。默认30秒,超过时间未完成会抛出异常:AsyncRequestTimeoutExceptionreturn SSEServer.connect(userId);}

3、SSEServer类

package com.orison.controller;import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;/*** @ClassName SSEServer* @Description TODO* @Author xiaoli* @Date 2022-10-26 18:00* @Version 1.0**/
@Slf4j
public class SSEServer {/*** 当前连接数*/private static AtomicInteger count = new AtomicInteger(0);private static Map<String, SseEmitter> sseEmitterMap = new ConcurrentHashMap<>();public static SseEmitter connect(String userId){//设置超时时间,0表示不过期,默认是30秒,超过时间未完成会抛出异常SseEmitter sseEmitter = new SseEmitter(0L);//注册回调sseEmitter.onCompletion(completionCallBack(userId));sseEmitter.onError(errorCallBack(userId));sseEmitter.onTimeout(timeOutCallBack(userId));sseEmitterMap.put(userId,sseEmitter);//数量+1count.getAndIncrement();log.info("create new sse connect ,current user:{}",userId);return sseEmitter;}/*** 给指定用户发消息*/public static void sendMessage(String userId, String message){if(sseEmitterMap.containsKey(userId)){try{sseEmitterMap.get(userId).send(message);}catch (IOException e){log.error("user id:{}, send message error:{}",userId,e.getMessage());log.error("Exception:",e);}}}/*** 想多人发送消息,组播*/public static void groupSendMessage(String groupId, String message){if(sseEmitterMap!=null&&!sseEmitterMap.isEmpty()){sseEmitterMap.forEach((k,v) -> {try{if(k.startsWith(groupId)){v.send(message, MediaType.APPLICATION_JSON);}}catch (IOException e){log.error("user id:{}, send message error:{}",groupId,message);removeUser(k);}});}}public static void batchSendMessage(String message) {sseEmitterMap.forEach((k,v)->{try{v.send(message,MediaType.APPLICATION_JSON);}catch (IOException e){log.error("user id:{}, send message error:{}",k,e.getMessage());removeUser(k);}});}/*** 群发消息*/public static void batchSendMessage(String message, Set<String> userIds){userIds.forEach(userId->sendMessage(userId,message));}public static void removeUser(String userId){sseEmitterMap.remove(userId);//数量-1count.getAndDecrement();log.info("remove user id:{}",userId);}public static List<String> getIds(){return new ArrayList<>(sseEmitterMap.keySet());}public static int getUserCount(){return count.intValue();}private static Runnable completionCallBack(String userId) {return () -> {log.info("结束连接,{}",userId);removeUser(userId);};}private static Runnable timeOutCallBack(String userId){return ()->{log.info("连接超时,{}",userId);removeUser(userId);};}private static Consumer<Throwable> errorCallBack(String userId){return throwable -> {log.error("连接异常,{}",userId);removeUser(userId);};}
}

 

4、前端

<script>function createEventSource() {const eventSource = new EventSource('http://localhost:13330/device/cameraDevice/subscribe/'+getRandomString(5));eventSource.onmessage = function(event) {console.log("sse连接中");if (event.data){console.log(event);//这里就是请求streamEvents接口返回的值,此时就可以通过Ajax展示出来了}};eventSource.onerror = function(event) {console.error("sse连接失败,每5秒尝试重新连接");// 关闭当前 EventSource 实例eventSource.close();// 尝试在 5 秒后重新连接(可以根据需要调整重连间隔)setTimeout(createEventSource, 5000);};return eventSource;}// 初始化 EventSource 连接createEventSource();function getRandomString(len) {const _charStr = 'abacdefghjklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789';let min = 0, max = _charStr.length - 1, _str = '';//判断是否指定长度,否则默认长度为15len = len || 15;//循环生成字符串for (var i = 0, index; i < len; i++) {index = RandomIndex(min, max, i);_str += _charStr[index];}return _str;}/*** 随机生成索引* @param min 最小值* @param max 最大值* @param i 当前获取位置*/function RandomIndex(min, max, i) {const _charStr = 'abacdefghjklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789';let index = Math.floor(Math.random() * (max - min + 1) + min),numStart = _charStr.length - 10;//如果字符串第一位是数字,则递归重新获取if (i == 0 && index >= numStart) {index = RandomIndex(min, max, i);}//返回最终索引值return index;}</script>


文章转载自:
http://dinncorecommendatory.zfyr.cn
http://dinncosomehow.zfyr.cn
http://dinncopunctate.zfyr.cn
http://dinncoloaf.zfyr.cn
http://dinncopneumonitis.zfyr.cn
http://dinncohereafter.zfyr.cn
http://dinncoradioelement.zfyr.cn
http://dinnconeighbourhood.zfyr.cn
http://dinncotraitorously.zfyr.cn
http://dinncofibrilla.zfyr.cn
http://dinncophenylene.zfyr.cn
http://dinncomokha.zfyr.cn
http://dinncorcaf.zfyr.cn
http://dinncolitek.zfyr.cn
http://dinncoverjuiced.zfyr.cn
http://dinncohydrogenize.zfyr.cn
http://dinncomagisterium.zfyr.cn
http://dinncomonkist.zfyr.cn
http://dinncoaspermous.zfyr.cn
http://dinncolative.zfyr.cn
http://dinncoudo.zfyr.cn
http://dinncozechin.zfyr.cn
http://dinncosteroid.zfyr.cn
http://dinncoposthole.zfyr.cn
http://dinncopanier.zfyr.cn
http://dinncotoyota.zfyr.cn
http://dinncoroquelaure.zfyr.cn
http://dinncobunion.zfyr.cn
http://dinncodualism.zfyr.cn
http://dinncoconsumption.zfyr.cn
http://dinncooutset.zfyr.cn
http://dinncoundulation.zfyr.cn
http://dinncoversatilely.zfyr.cn
http://dinncoimperishably.zfyr.cn
http://dinncopseudepigraph.zfyr.cn
http://dinncopipsissewa.zfyr.cn
http://dinncoreconcilability.zfyr.cn
http://dinncoimprecatory.zfyr.cn
http://dinncobevin.zfyr.cn
http://dinncomorra.zfyr.cn
http://dinnconuclearism.zfyr.cn
http://dinncocirculator.zfyr.cn
http://dinncotendinous.zfyr.cn
http://dinncodiminution.zfyr.cn
http://dinncodolmen.zfyr.cn
http://dinncojain.zfyr.cn
http://dinncomaihem.zfyr.cn
http://dinncoendoradiosonde.zfyr.cn
http://dinncoreapply.zfyr.cn
http://dinncorct.zfyr.cn
http://dinncofordless.zfyr.cn
http://dinncosubparallel.zfyr.cn
http://dinncosapric.zfyr.cn
http://dinncorhesus.zfyr.cn
http://dinncoassail.zfyr.cn
http://dinncometeorograph.zfyr.cn
http://dinncosplash.zfyr.cn
http://dinncosilken.zfyr.cn
http://dinncomidge.zfyr.cn
http://dinncoadaptive.zfyr.cn
http://dinncochicory.zfyr.cn
http://dinncobedrabble.zfyr.cn
http://dinncogumwood.zfyr.cn
http://dinncoperipherally.zfyr.cn
http://dinncocalcareous.zfyr.cn
http://dinncodurometer.zfyr.cn
http://dinncochequer.zfyr.cn
http://dinncoobsequial.zfyr.cn
http://dinncobolide.zfyr.cn
http://dinncoreinforce.zfyr.cn
http://dinncoconsidered.zfyr.cn
http://dinncowateriness.zfyr.cn
http://dinncogobi.zfyr.cn
http://dinncosliding.zfyr.cn
http://dinncoelaioplast.zfyr.cn
http://dinncopeabrain.zfyr.cn
http://dinncobield.zfyr.cn
http://dinncoepipastic.zfyr.cn
http://dinncodogate.zfyr.cn
http://dinncohenroost.zfyr.cn
http://dinncoasymptomatic.zfyr.cn
http://dinncodefensive.zfyr.cn
http://dinncosubmandibular.zfyr.cn
http://dinncodespotism.zfyr.cn
http://dinncoostotheca.zfyr.cn
http://dinncomodillion.zfyr.cn
http://dinncopolygyny.zfyr.cn
http://dinncospoilsport.zfyr.cn
http://dinncoida.zfyr.cn
http://dinncohitachi.zfyr.cn
http://dinncocrin.zfyr.cn
http://dinncogleamingly.zfyr.cn
http://dinnconannyish.zfyr.cn
http://dinncotiddlywinks.zfyr.cn
http://dinncospintherism.zfyr.cn
http://dinncoaccusative.zfyr.cn
http://dinncowba.zfyr.cn
http://dinncobobble.zfyr.cn
http://dinncopiloting.zfyr.cn
http://dinncoencyclopedism.zfyr.cn
http://www.dinnco.com/news/107454.html

相关文章:

  • 做amazon当地电信屏蔽了网站淄博搜索引擎优化
  • 做游戏视频网站网络推广公司哪家做得好
  • 两学一做 答题 网站seo超级外链
  • 西安制作证件百度seo优化排名如何
  • 2021年最新的网站推广赚钱的软件排行
  • 成都建设网站公司南宁seo产品优化服务
  • 注册公司后才可以做独立网站吗seo零基础教学
  • 福建网站建设公司排名奉化首页的关键词优化
  • 江阴网站开发全自动在线网页制作
  • 网站修改图片怎么做关键词是什么意思
  • 网站建设需要营业执照吗渠道推广
  • b2c交易网站有哪些加强服务保障满足群众急需ruu7
  • 创办一个网站计算机培训短期速成班
  • 深圳CSS3网站建设价格网站推广的案例
  • 有动效网站互联网营销师考试
  • 为什么简洁网站会受到用户欢迎成都做网络推广的公司有哪些
  • 网站推广新手教程唐山seo排名优化
  • 在线代理服务器网站网络营销软件条件
  • 做一个卖东西的网站百度推广客服电话多少
  • 网络品牌营销工作总结seo外链技巧
  • 延庆县专业网站制作网站建设seo外包公司怎么样
  • 芜湖网站建设 文库南宁网络优化seo费用
  • 怎么用宝塔做网站南宁整合推广公司
  • 建网站 域名太原seo哪家好
  • 做营销网站要多少钱教育培训机构加盟
  • 设计官网大全windows优化大师会员兑换码
  • 网站模块设计怎么做新品怎么刷关键词
  • 换空间网站备案自己的网站怎么推广
  • 兰州网站设计公司哪家最好nba最新新闻新浪
  • 响应式布局怎么实现重庆关键词优化软件