export function subMoving(onOpenFun, onMsgFun, onCloseFun) {
let lockReconnect = false;
var url = window._CONFIG['domianURL'].replace("https://", "wss://").replace("http://", "ws://") + "/threed_model/ws/warehouse/moving";
function initWebSocket() {
let ws = new WebSocket(url);
ws.addEventListener('open', (event) => {
console.log('已连接: ' + url);
if (onOpenFun != null) {
onOpenFun();
}
});
ws.addEventListener('message', (event) => {
if (onMsgFun != null) {
onMsgFun(event.data);
}
});
ws.addEventListener('close', (event) => {
console.log('已断开: ' + url);
if (onCloseFun != null) {
onCloseFun();
}
reconnect();
});
ws.addEventListener('error', (event) => {
console.log('发送错误: ' + url);
});
}
function reconnect() {
if (lockReconnect) return;
lockReconnect = true;
setTimeout(function () {
console.log('准备重连: ' + url);
initWebSocket();
lockReconnect = false;
}, 5000);
}
initWebSocket();
}