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

河北沧州网站建设发布新闻

河北沧州网站建设,发布新闻,360浏览器屏蔽某网站怎么做,河津北京网站建设使用原因:用户网络环境较差,之前使用ws总是出现断连重连,导致数据总是不能实时更新,所以更换为sse npm install event-source-polyfill createWebSocket:创建sse连接 getWebSocketMsg:接收sse消息 impo…

使用原因:用户网络环境较差,之前使用ws总是出现断连重连,导致数据总是不能实时更新,所以更换为sse

npm install event-source-polyfill

createWebSocket:创建sse连接 

getWebSocketMsg:接收sse消息

  import { EventSourcePolyfill } from "event-source-polyfill";import { getToken } from '@/utils/auth'class webSocketClass {constructor(name) {this.localUrl = `http`; //直连阿里云正式环境this.globalCallback = null;this.createWebSocket(name);this.readyState = 0;}createWebSocket(url) {var that =this// 建立连接this.eventSource = new EventSourcePolyfill(this.localUrl+ url,{// 设置重连时间heartbeatTimeout: 60 * 60 * 1000,// 添加tokenheaders: {Authorization: `Bearer ${getToken()}`,},});this.eventSource.onopen = (e) => {console.log("已建立SSE连接~");};this.eventSource.onmessage = (e) => {const d = JSON.parse(e.data);console.log("sse已接受到消息:", d);that.getWebSocketMsg(that.globalCallback);};this.eventSource.onerror = (e) => {console.log("SSE连接错误" + e.readyState);if (e.readyState == EventSource.CLOSED) {console.log("SSE连接关闭");} else if (this.eventSource.readyState == EventSource.CONNECTING) {console.log("SSE正在重连");//重新设置tokenthis.eventSource.headers = {Authorization: `Bearer ${getToken()}`,};} else {console.log("error", e);}};}getWebSocketMsg(callback) {console.log("开始接收sse消息~",this.eventSource);this.eventSource.onmessage = (ev) => {callback && callback(ev);};}close(){this.eventSource.close()console.log("SSE关闭" + e.readyState);}
}
export default webSocketClass;

使用方法:

  this.warningSSE = new vueSSEUtil('/sse/warning/'+this.userId);this.warningSSE.getWebSocketMsg((evt) => {const d = JSON.parse(evt.data);d.warnCode = this.code_to_value(d.warnCode);console.log('sse回调数据',d) });

一定要在页面退出关闭sse

this.warningSSE.close()

http://www.tj-hxxt.cn/news/125154.html

相关文章:

  • 合肥网络公司网站建设群发软件
  • wordPress回复表情找索引擎seo
  • 企业营销型展厅优势怎么做seo网站关键词优化
  • 升降平台联系企汇优做网站推广有什么好的推广平台
  • 北京个人网站制作包头网站建设推广
  • 做网站还挣钱吗网络销售技巧
  • 用Java或ssm做网站有什么区别重庆网页搜索排名提升
  • 成都网站设计推荐今日热点新闻头条国内
  • 网站源码对应的数据库怎么做网络培训平台
  • 软件前端开发需要学什么优化营商环境条例全文
  • wordpress打包app上架淄博seo网站推广
  • 文件网站建设营销管理培训课程培训班
  • 怎么做盗版小说网站做网页的网站
  • 网站外链多的危害百度收录量查询
  • 给企业做网站 工作seo工资一般多少
  • 烟台商城网站建设哈尔滨seo网络推广
  • 织梦中英文网站模板网上推广培训
  • 广州疫情最新公告关键词seo优化
  • 2345网址导航怎么关什么是关键词排名优化
  • 中英文的网站开发推广网络推广平台
  • 广东广东网站建设工作软件外包公司是什么意思
  • 网站制作多久seo推广一个月见效
  • 未备案 网站加速seo分析报告怎么写
  • 可以搜索附近手机的软件百度seo查询
  • 三叶草gy4591seo排名优化推广报价
  • net可以开发wordpress百度首页排名优化平台
  • 青岛城阳软件网站开发关键词搜索工具爱站网
  • 德州建设小学网站合肥网络公司seo建站
  • 8网站建设做网站爱站小工具圣经
  • 计算机个人网站建设论文百家号权重查询