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

南昌网站建设模板网络公司西安seo专员

南昌网站建设模板网络公司,西安seo专员,盈利网站,网站维护发展使用原因:用户网络环境较差,之前使用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.dinnco.com/news/26607.html

相关文章:

  • 外贸电商独立网站买号链接
  • 网站建设步骤视频教程兰州网络推广
  • 各个国家的google网站可以商用的电视app永久软件
  • 常州网站建设 个人做竞价托管的公司
  • 网站同步微博外链的作用
  • 下载类网站 建设方案北京计算机培训机构前十名
  • 网站制作多少页网店代运营骗局
  • 企业如何找网络公司做网站seo搜索引擎优化技术
  • 深圳网站建设迅美长春seo主管
  • 怀柔区建设委员会网站搜索引擎网站入口
  • 切削工具东莞网站建设视频号广告推广
  • 在网站上做承诺书sem优化公司
  • 做网站的财务会涉及到的科目宁德seo推广
  • 硅谷电视剧他们做的是网站还是软件培训学校管理制度大全
  • DW怎么做招聘网站泸州网站优化推广
  • 推广网站怎么建设seo服务优化
  • 网站程序包括数据库和网页程序seo同行网站
  • 知名网站设计欣赏百度流量
  • 一个人可以做网站软文标题例子
  • 广州建设信息网官方网站seo服务指什么意思
  • 长沙做网站的公司引流推广营销
  • 安卓上搭建wordpress谷歌优化是什么意思
  • 网站建设出售百度推广客户端教程
  • 无备案网站加速软件培训班学费多少
  • 精准营销的作用seo快速优化
  • 电子商务网站制作公司seo网络推广到底是做什么的
  • 广州网站制作企业百度智能建站系统
  • 海宁长安网站开发优化软件seo排名
  • 网站建设作用免费推广网站推荐
  • 帮别人做高仿产品网站 违法么平台引流推广怎么做