socket: null,
lockReconnect: false,
tt: "",
reconnect(service) {
this.tt && clearTimeout(this.tt);
if (this.limitConnect > 0) {
if (localStorage.getItem("lockReconnect") != true) {
localStorage.setItem("lockReconnect", 1);
this.limitConnect--;
this.timeConnect++;
console.log("第" + this.timeConnect + "次重连");
// 进行重连
this.tt = setTimeout(() => {
this.init(service);
localStorage.removeItem("lockReconnect");
}, 2000);
}
} else {
console.log("TCP连接已超时");
}
},
init(url) {
if (typeof WebSocket === "undefined") {
alert("您的浏览器不支持socket");
} else {
this.socket = new WebSocket(url);
this.socket.onopen = this.open;
this.socket.onerror = this.error;
this.socket.onmessage = this.getMessage;
}
},
open() {
console.log("socket连接成功");
},
error(e) {
console.log(e);
console.log("连接错误");
if (this.socket.readyState == 3) {
this.init(screenws);
}
this.reconnect(screenws);
},
getMessage(msg) {
console.log(msg, "返回的值");
},
close(e) {
console.log(e);
console.log("socket已经关闭");
this.reconnect(screenws);
},