heartChechInit() {
      const _this = this;
      // 设置统筹管理
      let heartCheck = {
        timer: 0, // 定时器名称
        _obj: null, // ws
        _callback: null, // 执行函数
        _time: 30000, // 心跳间隔
        // 启动函数
        init: function (wsObj, callback) {
          // console.log("init");
          this._obj = wsObj;
          callback && (this._callback = callback);
          this.sayHi();
        },
        sayHi: function () {
          // 执行心跳
          clearTimeout(this.timer);
          this.timer = setTimeout(() => {
            if (1 == this._obj.readyState) {
              this._obj.send(1); // 发送讯息
            }
          }, this._time);
        },
        clear: function (flag) {
          // 关闭
          // console.log("clear:" + this.timer);
          clearTimeout(this.timer);
        },
        onError: function () {
          // 出错
          // console.log("onError:", this.timer);
          this.clear();
          this._callback && this._callback();
        },
      };
      // 通讯地址
      let uri = `ws://${sessionStorage.getItem("heartCheckUrl")}/flow/notice/${
        _this.currentUserID
      }`;
      let ws = new WebSocket(uri);
      // 开始连接
      ws.onopen = (event) => {
        // console.log("ws onopen", event);
        MsgBegin && MsgBegin();
        heartCheck.init(ws, () => {
          console.log("reconnect...");
          ws = new WebSocket(uri);
        });
      };
      // 接收消息
      ws.onmessage = (event) => {
        // console.log("接收消息", event, ws);
        let reg = /[0-9]/;
        if (reg.test(event.data)) {
          _this.value = +event.data;
        } else {
          _this.value = 0;
        }
        heartCheck.sayHi();
      };
      // 连接关闭
      ws.onclose = (event) => {
        // console.log("ws close", event, ws);
        heartCheck.clear();
      };
      // 连接出错
      ws.onerror = (event) => {
        _this.value = 0;
        // console.log("ws error", event, ws);
        heartCheck.onError();
      };
      // 初始请求接收
      let MsgBegin = () => {
        ws.send(1);
      };
    },

最新文章

  1. the user operation is waiting
  2. JSON和JSONP (含jQuery实例)(share)
  3. 【bzoj1082】 SCOI2005—栅栏
  4. struts2 javaweb 过滤器、监听器 拦截器 原理
  5. iscroll简单使用说明
  6. JavaScriptSerializer.MaxJsonLength属性问题
  7. leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)
  8. Codeforces13C–Sequence(区间DP)
  9. 高逼格的实现WiFi共享,不安装第三方wifi共享软件,两种方式实现开启wifi的功能
  10. HTML5学习笔记简明版(11):新API
  11. Effective C++ Item 46 当需要投你非成员函数定义模板
  12. Spring MVC 的环境搭建和入门小程序
  13. 第1 章MySQL 基本介绍
  14. JasperReport的安装
  15. WebLogic及其他
  16. @RequestParam Map<String, Object> paramMap
  17. 20175209 《Java程序设计》第五周学习总结
  18. Shiro笔记(四)Shiro的realm认证
  19. nodemcu使用心得1
  20. windows下vue开发环境的搭建

热门文章

  1. openstack horizon 学习(3) DataTable
  2. SpringBoot使用IDEA设置的外部Tomcat启动
  3. axios ajax fetch 区别以及优缺点
  4. PHP中的单引号跟双引号的区别
  5. 扩展NSDate类实现快捷使用 —— 昉
  6. 按照递推的思想求解next[]数组
  7. 简单模拟Java中反射的应用场景
  8. 解决OpenOCD烧录STM32失败, 无法通过SWD连接的问题
  9. netstat 竟然还能这么玩儿?
  10. 二叉树的基本操作(C语言版)