let ws = null;
let wsTime = 10; // 重连次数
const wsUrl = "ws://www.asai.cc:端口号";
function wsInit() {
ws.onopen = function () {
wsHeart.reStart();
};
ws.onmessage = function (e) {
wsHeart.reStart();
if (e.data === "pong") {
console.log(666.909, "pong");
} else {
// 处理正常消息的地方
}
};
ws.onclose = function () {
if (wsTime > 0) {
wsHeart.reConnect();
} else {
wsHeart.reClose();
}
};
ws.onerror = function () {
if (wsTime > 0) {
wsHeart.reConnect();
} else {
wsHeart.reClose();
}
};
window.onbeforeunload = function () {
wsHeart.reClose();
};
}
var wsHeart = {
timeout: 3000, // 心跳延迟时间
timeoutObj: null,
serverTimeoutObj: null,
connectTimeoutObj: null,
lockConnect: false,
lockConnectTimeout: 2000, // 重连延迟时间
reSet: function () {
wsTime = 10;
clearTimeout(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
clearTimeout(this.connectTimeoutObj);
this.timeoutObj = null;
this.serverTimeoutObj = null;
this.connectTimeoutObj = null;
},
reClose: function () {
this.reSet();
ws.close();
},
reStart: function () {
this.reSet();
this.reCheck();
},
reCheck: function () {
this.timeoutObj = setTimeout(() => {
ws.send("ping");
this.serverTimeoutObj = setTimeout(() => {
this.reClose();
}, this.timeout);
}, this.timeout);
},
reConnect: function () {
if (!this.lockConnect) {
wsTime--;
this.lockConnect = true;
this.connectTimeoutObj = setTimeout(() => {
this.newWs();
this.lockConnect = false;
}, this.lockConnectTimeout);
}
},
newWs: function () {
try {
if ("WebSocket" in window) {
ws = new WebSocket(wsUrl);
wsInit();
}
} catch (e) {
this.reConnect();
}
},
};
wsHeart.newWs();
|