websocket连接url,获取进度条数据

这篇博客介绍了如何在JavaScript中实现WebSocket的连接和错误处理。当浏览器支持WebSocket时,它会建立到指定URL的连接,并在打开、错误、消息接收和关闭时触发相应事件。在连接关闭或出现错误时,会进行重连操作,确保连接的稳定性。同时,还提供了一个公共的WebSocket连接获取函数,允许自定义回调和页面URL。
摘要由CSDN通过智能技术生成

当连接一个url的时候,我们就按照传统的方式写就可以了

 initWebSocket: function () {
      if ("WebSocket" in window){
        let url ='ws://'+ this.baseUrl.split('//')[1] +'/TotalEmergeProgressWebsocket'
        console.log(url)
        this.websock = new WebSocket(url);
      }
      else{
        alert('该浏览器不支持websocket')
      }
      this.websock.onopen = this.websocketOnopen;
      this.websock.onmessage = this.websocketOnmessage;
      this.websock.onerror = this.websocketOnerror;
      this.websock.onclose = this.websocketOnclose;
    },
    websocketOnopen: function () {
      console.log("WebSocket连接成功");
      // 设置定时器
      // 如果状态为正在爬取就设置定时器
      //心跳检测重置
      // this.heartCheck.reset().start();
    },
    websocketOnerror: function (e) {
      console.log("WebSocket连接发生错误");
      this.reconnect();
    },
    websocketOnmessage: function (e) {
      this.loading = true
      console.log("-----接收消息-------",e.data);
      var data = eval("(" + e.data + ")"); //解析对象
      console.log(this.percentage)
      this.percentage = (data.emergedDataNum/data.totalDataNum).toFixed(1)*100;
      // 如果进度到了100%
      if(data.totalDataNum === (data.emergedDataNum+data.errorTableNum)){
        //关闭websocket连接,并且请求失败的数据
        this.websock.close();
        this.loading = false
        this.getFailDB()
      }
    },
    websocketOnclose: function (e) {
      console.log("connection closed (" + e.code + ")");
      this.reconnect();
    },
    websocketSend(text) { // 数据发送
      try {
        this.websock.send(text);
      } catch (err) {
        console.log("send failed (" + err.code + ")");
      }
    },
    reconnect() {
      var that = this;
      if(that.lockReconnect) return;
      that.lockReconnect = true;
      //没连接上会一直重连,设置延迟避免请求过多
      setTimeout(function () {
        console.info("尝试重连...");
        that.initWebSocket();
        that.lockReconnect = false;
      }, 20000);
    },

我们把它定义成公共类。

import { ElMessage } from 'element-plus'
let wsUrl = `ws://localhost:8082/TableEmergeProgressWebsocket`
let lockReconnect = true;
let callback = null;
let socket = null;
function getConnect(callback,pageUrl){
    if ("WebSocket" in window){
        const requestWsUrl = wsUrl + pageUrl;
        console.log(requestWsUrl)
        socket = new WebSocket(requestWsUrl);
    }
    else{
        alert('该浏览器不支持websocket')
    }
    socket.onmessage = function (e) {
        callback(e,socket);
    }
    socket.onopen = function () {
        console.log("WebSocket子表连接成功");
    }
    socket.onerror = function () {
        ElMessage.error('ws连接发生错误');
        reconnect(pageUrl);
    }
    socket.onclose = function (e) {
        console.log("connection closed (" + e.code + ")");
    }
}
function reconnect(pageUrl){
    if(lockReconnect) return;
    lockReconnect = true;
    setTimeout(function () {
        console.info("尝试重连...");
        this.getConnect(callback,pageUrl);
        lockReconnect = false;
    }, 20000);
}

export default getConnect



在具体的页面组件重写回调函数
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值