websocket心跳函数

调用
new WebSocket(url).onmessage=()=>{
heartbeat.reset();
}

//心跳
const heartbeat = new function () {
this.timeout = 18000;
this.timeoutObj = null;
this.reset = function () {
clearTimeout(this.timeoutObj);
this.start();
}
this.start = function () {
this.timeoutObj = setTimeout(function () {
ws.send("heartbeat");
this.reset()
}.bind(this), this.timeout);
}
}

你可能感兴趣的:(websocket心跳函数)