import { v4 as uuidv4 } from "uuid"; import cuttingHandler from "./cutting"; import energeHandler from "./energe"; import IsHandler from "./IS"; import dcsHandler from "./dcs"; import smokeHandler from "./smoke"; /** * new XClient('ws://192.168.1.12:8081/xc-screen/websocket/xc001', 'DCS') */ class XClient { // url; // name; // ws; // timeout = 1000 * 5; // heartbeatDelay = 1000 * 60 * 3; // reconnectCount = 0; // onmessage = (msg) => console.log(msg); // hb = 0; constructor(config, onmessage = (msg) => console.log(msg)) { this.url = config.url; this.name = config.name; this.ws = new WebSocket(config.url); this.timeout = config.timeout || 1000 * 5; this.heartbeatDelay = 1000 * 60 * 3; this.reconnectCount = 0; this.ws.onopen = () => { console.log(`[*] ${this.name} 初始化连接成功`); this.hb = this.heartbeat(); }; this.onmessage = onmessage; this.ws.onmessage = onmessage; this.ws.onerror = (err) => { console.log(`[*] ${this.name} 连接错误`, err); this.tryReconnect(); }; this.ws.onclose = (e) => { console.log(`[*] ${this.name} 连接关闭`); }; } heartbeat() { if (this.hb) clearInterval(this.hb); return setInterval(() => { console.log(`${this.name} ping...`); if (this.ws.readyState == WebSocket.OPEN) { this.ws.send("ping"); } else { clearInterval(this.hb); this.tryReconnect(); } }, this.heartbeatDelay); } tryReconnect() { console.log(`[*] ${this.name} 重连中,已尝试:`, this.reconnectCount, "次"); setTimeout( () => { if (this.ws.readyState === WebSocket.OPEN) { this.reconnectCount = 0; console.log(`[*] ${this.name} 已恢复连接,取消重连`); return; } this.reconnectCount++; this.ws = new WebSocket(this.url); this.ws.onopen = () => { console.log(`[*] ${this.name} 重连成功`); this.reconnectCount = 0; this.hb = this.heartbeat(); }; this.ws.onmessage = this.onmessage; this.ws.onerror = (err) => { console.log(`[*] ${this.name} 重连出错`); this.tryReconnect(); }; this.ws.onclose = (e) => { console.log(`[*] ${this.name} 重连连接关闭`); }; }, this.reconnectCount == 0 ? 0 : this.timeout ); } logStatus() { console.log( `[*] ${this.name} 重连状态: 连接中`, this.ws.readyState == WebSocket.CONNECTING ); console.log( `[*] ${this.name} 重连状态: 已连接`, this.ws.readyState == WebSocket.OPEN ); console.log( `[*] ${this.name} 重连状态: 关闭中`, this.ws.readyState == WebSocket.CLOSING ); console.log( `[*] ${this.name} 重连状态: 关闭`, this.ws.readyState == WebSocket.CLOSED ); } } const newUser = uuidv4(); new XClient( { // url: "ws://10.70.180.10:8081/xc-screen/websocket/xc001" + newUser, url: "ws://127.0.0.1:9800/alarm?user=" + newUser, name: "DCS_DATA", // "ws://m306416y13.yicp.fun:35441/xc-screen/websocket/xc001", // "ws://192.168.1.114:8081/xc-screen/websocket/xc001", // "ws://192.168.1.12:8081/xc-screen/websocket/xc001", }, dcsHandler ); new XClient( { // url: "ws://192.168.1.20:48080/websocket/message?userId=ENERGY" + newUser, url: "ws://10.70.2.2:8080/websocket/message?userId=ENERGY" + newUser, // url: "ws://192.168.1.74:48080/websocket/message?userId=ENERGY" + newUser, name: "MES_DATA", // "ws://192.168.1.74:48080/websocket/message?userId=ENERGY111", }, energeHandler ); // 产线缺陷相关数据 new XClient( { url: "ws://10.70.2.2:8080/websocket/message?userId=IS" + newUser, name: "QUALITY_DATA", // "ws://192.168.0.33:48082/websocket/message?userId=IS111", }, IsHandler ); // 良品率相关数据 new XClient( { url: "ws://10.70.2.2:8080/websocket/message?userId=CUTTING" + newUser, name: "CUTTING_DATA", // "ws://10.70.27.122:8080/websocket/message?userId=CUTTING", // "ws://192.168.0.33:48082/websocket/message?userId=CUTTING" + newUser, }, cuttingHandler ); // 烟气处理相关数据 new XClient( { url: "ws://10.70.2.2:8080/websocket/message?userId=GAS" + newUser, name: "SMOKE_DATA", // "ws://10.70.27.122:8080/websocket/message?userId=CUTTING", // "ws://192.168.1.62:48082/websocket/message?userId=GAS" + newUser, }, smokeHandler ); 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; }