import { isJSON } from "./utils.js";
import { socketUrl } from "../base.js";
class WebSocketClass {
constructor(url = socketUrl) {
this.lockReconnect = false;
this.wsUrl = "";
this.globalCallback = null;
this.userClose = false;
this.createWebSocket(url);
this.timeout = 30000;
this.timeoutObj = null;
this.serverTimeoutObj = null;
}
initEventHandle() {
this.ws.onopen = (event) => {
this.start();
console.log("WebSocket连接打开");
};
this.ws.onOpen((res) => {
this.start();
console.log("WebSocket连接打开");
});
this.ws.onclose = (event) => {
if (!this.userClose) {
this.reconnect(this.wsUrl);
}
};
this.ws.onClose(() => {
if (!this.userClose) {
this.reconnect(this.wsUrl);
}
});
this.ws.onerror = (event) => {
this.reconnect(this.wsUrl);
};
this.ws.onError(() => {
this.reconnect(this.wsUrl);
});
this.ws.onmessage = (event) => {
if (isJSON(event.data)) {
const jsonobject = JSON.parse(event.data);
this.globalCallback(jsonobject);
} else {
this.globalCallback(event.data);
}
this.start();
};
this.ws.onMessage((event) => {
console.log("收到消息", event.data);
if (isJSON(event.data)) {
const jsonobject = JSON.parse(event.data);
this.globalCallback(jsonobject);
} else {
this.globalCallback(event.data);
}
this.start();
});
}
createWebSocket(url) {
if (typeof WebSocket === "undefined") {
this.writeToScreen("您的浏览器不支持WebSocket,无法获取数据");
return false;
}
if (typeof uni.connectSocket === "undefined") {
this.writeToScreen("您的浏览器不支持WebSocket,无法获取数据");
return false;
}
this.wsUrl = url;
try {
this.ws = new WebSocket(this.wsUrl);
this.initEventHandle();
this.ws = uni.connectSocket({
url: this.wsUrl,
success: (data) => {
console.log("websocket连接成功");
this.start();
this.initEventHandle();
},
});
} catch (e) {
this.reconnect(url);
}
}
reconnect(url) {
if (this.lockReconnect) return;
this.ws.close();
this.lockReconnect = true;
setTimeout(() => {
this.createWebSocket(url);
this.lockReconnect = false;
}, 1000);
}
webSocketSendMsg(msg) {
this.ws &&
this.ws.send({
data: msg,
success: () => {
console.log("消息发送成功");
},
fail: (err) => {
console.log("关闭失败", err);
},
});
}
getWebSocketMsg(callback) {
this.globalCallback = callback;
}
closeSocket() {
if (this.ws) {
this.userClose = true;
this.ws.close({
success: (res) => {
console.log("关闭成功", res);
},
fail: (err) => {
console.log("关闭失败", err);
},
});
}
}
writeToScreen(massage) {
console.log(massage);
}
start() {
this.timeoutObj && clearTimeout(this.timeoutObj);
this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj);
this.timeoutObj = setTimeout(() => {
this.writeToScreen("心跳检查,发送ping到后台");
try {
const datas = { ping: true };
this.webSocketSendMsg(JSON.stringify(datas));
} catch (err) {
this.writeToScreen("发送ping异常");
}
this.serverTimeoutObj = setTimeout(() => {
this.writeToScreen("没有收到后台发送得消息, 重新连接")
this.reconnect(this.wsUrl)
}, this.timeout)
}, this.timeout)
}
reset() {
clearTimeout(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
this.start();
}
stop() {
clearTimeout(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
}
}
export default WebSocketClass;