sailei1 / blog

1 stars 0 forks source link

websocket连接 切换浏览器窗户 防止自动断开 #109

Closed sailei1 closed 3 years ago

sailei1 commented 3 years ago
var worker_script = '';
        worker_script += 'var aaa = 0;';
        worker_script += 'function timedCount()';
        worker_script += '{';
        worker_script += '  aaa=aaa+1;';
        worker_script += '  postMessage(aaa);';
        // worker_script += "   var time = new Date();";
        // worker_script += "   console.log(\"[heartbeat time:\" + time.toLocaleTimeString() + \"] \");";
        worker_script += '  setTimeout("timedCount()",20000);';
        worker_script += '}';
        worker_script += 'timedCount();';
        var worker_blob = new Blob([worker_script], {
          type: 'text/javascript',
        });
        var worker_src = URL.createObjectURL(worker_blob);

        if (typeof Worker !== 'undefined') {
          if (typeof w == 'undefined') {
            w = new Worker(worker_src);
          }
          w.onmessage = function (event) {
            // console.log("支持 Web Workers...");
            self.__send({ class: 'general', method: 'heartbeat', params: null });
            var time = new Date();
            // console.log("[heartbeat time:" + time.toLocaleTimeString() + ",state-" + self.agent_state + "] ");
          };
        } else {
          // console.log("抱歉,你的浏览器不支持 Web Workers...");
        ...
        }