xuchang-new/src/utils/index.js

38 lines
958 B
JavaScript
Raw Normal View History

2023-11-09 13:36:21 +08:00
export function randomInt(min, max, includeMax = false) {
let Fn = includeMax ? Math.ceil : Math.floor;
let num = Fn(Math.random() * max);
while (num < min) {
num = Fn(Math.random() * max);
}
return num;
}
export class WsClient {
static wsServer = 'ws://192.168.1.12:8081/xc-screen/websocket/1';
static socket = null;
static tryCount = 0;
init() {
if (WsClient.socket) return;
WsClient.socket = new WebSocket(WsClient.wsServer);
WsClient.socket.onopen = () => {
console.log('[*] websocket connected!');
};
WsClient.socket.onmessage = (e) => {
console.log('[*] websocket message!', e, e.data);
};
WsClient.socket.onerror = (e) => {
console.log('[*] websocket erro!', e, e.data);
};
WsClient.socket.onclose = (e) => {
let timer = setTimeout(() => {
if (WsClient.tryCount < 3) {
const wsc = new WsClient();
wsc.init();
WsClient.tryCount += 1;
} else clearTimeout(timer);
}, 30000);
};
}
}