update websocket
This commit is contained in:
parent
5f92580c7e
commit
42e5bea184
@ -9,23 +9,36 @@ export function randomInt(min, max, includeMax = false) {
|
||||
return num;
|
||||
}
|
||||
|
||||
export class WsClient {
|
||||
static wsServer = "ws://192.168.1.12:8081/xc-screen/websocket/xc001";
|
||||
static socket = null;
|
||||
static tryCount = 0;
|
||||
|
||||
constructor() {
|
||||
if (WsClient.socket) return;
|
||||
WsClient.socket = new WebSocket(WsClient.wsServer);
|
||||
WsClient.socket.onopen = () => {
|
||||
console.log("[*] websocket connected!");
|
||||
/**
|
||||
* new XClient('ws://192.168.1.12:8081/xc-screen/websocket/xc001', 'DCS')
|
||||
*/
|
||||
class XClient {
|
||||
constructor(url, name, onmessage) {
|
||||
this.url = url;
|
||||
this.name = name;
|
||||
this.ws = new WebSocket(url);
|
||||
this.ws.onopen = () => {
|
||||
console.log(`[*] ${this.name} ws connected`);
|
||||
};
|
||||
WsClient.socket.onmessage = (e) => {
|
||||
this.ws.onmessage = onmessage;
|
||||
this.ws.onerror = (err) => {
|
||||
console.log("[*] websocket error!", err, err.data);
|
||||
};
|
||||
this.ws.onclose = (e) => {
|
||||
console.log(`[*] ${this.name} ws closed`);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
new XClient(
|
||||
"ws://192.168.1.12:8081/xc-screen/websocket/xc001",
|
||||
"DCS_DATA",
|
||||
(msg) => {
|
||||
let serializedData = null;
|
||||
try {
|
||||
serializedData = JSON.parse(e.data);
|
||||
serializedData = JSON.parse(msg.data);
|
||||
} catch (error) {
|
||||
console.log("[*] websocket: [unable to serialize] ---> ", e);
|
||||
console.log("[*] websocket: [unable to serialize] ---> ", msg);
|
||||
}
|
||||
switch (serializedData?.type) {
|
||||
case "KilnInfo": {
|
||||
@ -129,20 +142,34 @@ export class WsClient {
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
console.log("websocket message: [unknown] ---> ", e.data);
|
||||
console.log("websocket message: [unknown] ---> ", msg.data);
|
||||
}
|
||||
}
|
||||
};
|
||||
WsClient.socket.onerror = (e) => {
|
||||
console.log("[*] websocket error!", e, e.data);
|
||||
};
|
||||
WsClient.socket.onclose = (e) => {
|
||||
let timer = setTimeout(() => {
|
||||
if (WsClient.tryCount < 3) {
|
||||
new WsClient();
|
||||
WsClient.tryCount += 1;
|
||||
} else clearTimeout(timer);
|
||||
}, 30000);
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
new XClient(
|
||||
"ws://192.168.1.74:48080/websocket/message?userId=ENERGY111",
|
||||
"MES_DATA",
|
||||
(msg) => {
|
||||
let serializedData = null;
|
||||
try {
|
||||
serializedData = JSON.parse(msg.data);
|
||||
} catch (error) {
|
||||
console.log("[*] websocket: [unable to serialize] ---> ", msg);
|
||||
}
|
||||
switch (serializedData?.type) {
|
||||
case "EnergyInfo": {
|
||||
// 能耗数据
|
||||
store.dispatch({
|
||||
type: "energy/setInfo",
|
||||
payload: serializedData.data,
|
||||
});
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
console.log("websocket message: [unknown] ---> ", msg.data);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user