add fake websocket server

This commit is contained in:
lb 2024-04-19 16:38:41 +08:00
джерело dff017a5a2
коміт e854b966dc
5 змінених файлів з 302 додано та 28 видалено

@ -0,0 +1,120 @@
const total = `AN_1_1,助燃风故障报警
AN_1_2,压延机冷却风故障报警
AN_1_3,L吊墙冷却风机故障报警
AN_1_4,熔化带及部分澄清部冷却风机故障报警
AN_2_1,澄清带池壁冷却风机故障报警
AN_2_2,钢碴碴冷却风机故障报警
AN_2_3,支通路拐角冷却风机报警
AN_2_4,吊墙吊钩砖结构温升报警
AN_3_1,深层水包冷却水温升报警
AN_3_2,深层水包冷却水断水报警
AN_3_3,卡脖吊平冷却水断水报警
AN_3_4,卡脖吊平喧冷却水温升报警
AN_4_1,液面计冷却水断水报警
AN_4_2,液面计冷却水温升报警
AN_4_3,循环水入口压力低报警
AN_4_4,天然气压力高低报警
AN_5_1,助燃风风压低报警
AN_5_2,净化压缩气压力低报警
AN_5_3,普通压缩空气压力高低报警
AN_5_4,焦炉煤气气压力高低报警
AN_6_1,换向过程故障报警
AN_6_2,空交机换向不到位报警
AN_6_3,投料机故障报警
AN_6_4,备用
AN_7_1,1#压延机冷却水断水报警
AN_7_2,1#压延机冷却水温升报警
AN_7_3,1#过渡辊台冷却水断水报警
AN_7_4,1过渡辊台冷却水温升报警
AN_8_1,1#线唇砖冷却水断水报警
AN_8_2,1#线唇砖冷却水温升报警
AN_8_3,1#退火窑A区风机报警
AN_8_4,1#退火窑B区风机报警
AN_9_1,1#退火窑c区风机报警
AN_9_2,1#退火窑Ret1区风机报警
AN_9_3,1#退火窑Ret2区风机报警
AN_9_4,1#退火窑F1区风机报警
AN_10_1,1#退火窑F2区风机报警
AN_10_2,1#主传动报警
AN_10_3,1#压延机报警
AN_10_4,备用
AN_11_1,2#压延机冷却水断水报警
AN_11_2,2#压延机冷却水温升报警
AN_11_3,2#过渡辊台冷却水断水报警
AN_11_4,2#过渡辊台冷却水温升报警
AN_12_1,2#线唇砖冷却水断水报警
AN_12_2,2#线唇砖冷却水温升报警
AN_12_3,2#退火窑A区风机报警
AN_12_4,2#退火窑B区风机报警
AN_13_1,2#退火窑c区风机报警
AN_13_2,2#退火窑Ret1区风机报警
AN_13_3,2#退火窑Ret2区风机报警
AN_13_4,2#退火窑F1区风机报警
AN_14_1,2#退火窑F2区风机报警
AN_14_2,2#主传动报警
AN_14_3,2#压延机报警
AN_14_4,备用
AN_15_1,3#压延机冷却水断水报警
AN_15_2,3压延机冷却水温升报警
AN_15_3,3#过渡辊台冷却水断水报警
AN_15_4,3#过渡辊台冷却水温升报警
AN_16_1,3#线唇砖冷却水断水报警
AN_16_2,3#线唇砖冷却水温升报警
AN_16_3,3#退火窑A区风机报警
AN_16_4,3#退火窑B区风机报警
AN_17_1,3#退火窑c区风机报警
AN_17_2,3#退火窑Ret1区风机报警
AN_17_3,3#退火窑Ret2区风机报警
AN_17_4,3#退火窑F1区风机报警
AN_18_1,3#退火窑F2区风机报警
AN_18_2,3#主传动报警
AN_18_3,3#压延机报警
AN_18_4,备用
AN_19_1,4#压延机冷却水断水报警
AN_19_2,4压延机冷却水温升报警
AN_19_3,4#过渡辊台冷却水断水报警
AN_19_4,4#过渡辊台冷却水温升报警
AN_20_1,4#线唇砖冷却水断水报警
AN_20_2,4#线唇砖冷却水温升报警
AN_20_3,4#退火窑A区风机报警
AN_20_4,4#退火窑B区风机报警
AN_21_1,4#退火窑c区风机报警
AN_21_2,4#退火窑Ret1区风机报警
AN_21_3,4#退火窑Ret2区风机报警
AN_21_4,4#退火窑F1区风机报警
AN_22_1,4#退火窑F2区风机报警
AN_22_2,4#主传动报警
AN_22_3,4#压延机报警
AN_23_1,5#压延机冷却水断水报警
AN_23_2,5#压延机冷却水温升报警
AN_23_3,5#过渡辊台冷却水断水报警
AN_23_4,5#过渡台冷却水温升报警
AN_24_1,5#线唇砖冷却水断水报警
AN_24_2,5#线唇砖冷却水温升报警
AN_24_3,5#退火窑A区风机报警
AN_24_4,5#退火窑B区风机报警
AN_25_1,5#退火窑c区风机报警
AN_25_2,5#退火窑Ret1区风机报警
AN_25_3,5#退火窑Ret2区风机报警
AN_25_4,5#退火窑F1区风机报警
AN_26_1,5#退火窑F2区风机报警
AN_26_2,5#主传动报警
AN_26_3,5#压延机报警
`;
// const totalAlarmList = total.split("\n");
const getRandomAlarmList = function (num) {
const totalAlarmList = total.split("\n");
let alarmList = [];
for (let i = 0; i < num; i++) {
let index = Math.floor(Math.random() * totalAlarmList.length);
const chosen = totalAlarmList[index];
totalAlarmList.splice(index, 1);
alarmList.push(chosen);
}
return alarmList;
};
module.exports = {
getRandomAlarmList,
};

44
websocket/package-lock.json згенерований Normal file

@ -0,0 +1,44 @@
{
"name": "ws-server",
"version": "0.0.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "ws-server",
"version": "0.0.1",
"license": "ISC",
"dependencies": {
"ws": "^8.16.0"
}
},
"node_modules/ws": {
"version": "8.16.0",
"resolved": "https://registry.npmmirror.com/ws/-/ws-8.16.0.tgz",
"integrity": "sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==",
"engines": {
"node": ">=10.0.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": ">=5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
}
},
"dependencies": {
"ws": {
"version": "8.16.0",
"resolved": "https://registry.npmmirror.com/ws/-/ws-8.16.0.tgz",
"integrity": "sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==",
"requires": {}
}
}
}

15
websocket/package.json Normal file

@ -0,0 +1,15 @@
{
"name": "ws-server",
"version": "0.0.1",
"description": "a simple dev ws-server",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"ws": "^8.16.0"
}
}

105
websocket/server copy.js Normal file

@ -0,0 +1,105 @@
import { WebSocket, WebSocketServer } from "ws";
import template from "./template.json";
import utils from "./utils";
const wss = new WebSocketServer({ port: 9800 });
const frequency = 10; // seconds
const frequency1 = 1200; // seconds
wss.on("connection", function (ws, req) {
// console.log("ws", ws);
console.log(
"Client in: ",
req.socket.remoteAddress,
"current users:",
wss.clients.size
);
// ws.on("error", console.error);
// ws.emit("message", "connected");
ws.on("open", function () {
console.log("connected");
ws.send("connected");
});
ws.on("message", function (msg) {
console.log("message from client", msg);
ws.send("echo " + msg.toString());
});
ws.on("error", console.error);
const timer = setInterval(() => {
sendMsg(ws, "kiln-info"); // 窑炉信息
sendMsg(ws, "energy-cost"); // 运行状态
sendMsg(ws, "run-state"); // 运行状态
sendMsg(ws, "realtime");
sendMsg(ws, "his-trend");
// sendMsg(ws, 'gas');
// sendMsg(ws, 'kiln-top');
// sendMsg(ws, 'kiln-bottom');
}, frequency * 1000);
const timer1 = setInterval(() => {
// sendMsg(ws, 'run-state'); // 运行状态
}, frequency1 * 1000);
ws.on("close", function () {
console.log("停止监听");
clearInterval(timer);
clearInterval(timer1);
});
});
function sendMsg(ws, type) {
let data = {};
switch (type) {
case "kiln-info":
for (const key in template.kilnInfo) {
data[key] = utils.getRandom(template.kilnInfo[key]);
}
break;
case "energy-cost":
for (const key in template.energyCost) {
data[key] = utils.getRandom(template.energyCost[key]);
}
break;
case "run-state":
data = template.runState;
data.fireDirection =
Math.floor(Math.random() * 10) % 2 === 0 ? "南火" : "北火";
break;
case "realtime":
/** 天然气 实时流量 */
data.gasii = template.realtime.gasii.map((v) => utils.getRandom(v));
/** 助燃风 实时流量 */
data.wind = template.realtime.wind.map((v) => utils.getRandom(v));
break;
case "his-trend":
const hisTrend = {
gas: {},
wind: {},
};
Object.keys(template.hisTrend.gas).forEach((key) => {
hisTrend.gas[key] = template.hisTrend.gas[key].map((v) =>
utils.getRandom(v)
);
});
Object.keys(template.hisTrend.wind).forEach((key) => {
hisTrend.wind[key] = template.hisTrend.wind[key].map((v) =>
utils.getRandom(v)
);
});
data = hisTrend;
break;
case "gas":
// data = template.gas;
break;
case "kiln-top":
// data = template.kilnTop;
break;
case "kiln-bottom":
// data = template.kilnBottom;
break;
default:
// data = 'You are connected!';
break;
}
ws.send(JSON.stringify({ type, data }));
}

@ -1,55 +1,45 @@
import { WebSocket, WebSocketServer } from "ws";
import template from "./template.json";
import utils from "./utils";
const { WebSocket } = require("ws");
const { getRandomAlarmList } = require("./fakedata/alarmList");
const wss = new WebSocket.Server({ port: 9800 });
console.log('[websocket] server created');
const wss = new WebSocketServer({ port: 9800 });
const frequency = 10; // seconds
const frequency1 = 1200; // seconds
wss.on("connection", function (ws, req) {
// console.log("ws", ws);
console.log(
"Client in: ",
"[用户连接]: ",
req.socket.remoteAddress,
"current users:",
"当前总用户数:",
wss.clients.size
);
// ws.on("error", console.error);
// ws.emit("message", "connected");
ws.on("open", function () {
console.log("connected");
console.log('[websocket] opened');
ws.send("connected");
});
ws.on("message", function (msg) {
console.log("message from client", msg);
ws.send("echo " + msg.toString());
console.log("[==>]", msg);
});
ws.on("error", console.error);
const timer = setInterval(() => {
sendMsg(ws, "kiln-info"); // 窑炉信息
sendMsg(ws, "energy-cost"); // 运行状态
sendMsg(ws, "run-state"); // 运行状态
sendMsg(ws, "realtime");
sendMsg(ws, "his-trend");
// sendMsg(ws, 'gas');
// sendMsg(ws, 'kiln-top');
// sendMsg(ws, 'kiln-bottom');
}, frequency * 1000);
const timer1 = setInterval(() => {
// sendMsg(ws, 'run-state'); // 运行状态
}, frequency1 * 1000);
sendMsg(ws, "alarmList"); // 报警信息
}, 10 * 1000);
ws.on("close", function () {
console.log("停止监听");
clearInterval(timer);
clearInterval(timer1);
});
});
function sendMsg(ws, type) {
let data = {};
switch (type) {
case "alarmList":
const alarmList = getRandomAlarmList(Math.floor(Math.random() * 10) + 1);
alarmList.forEach((str) => {
const [code, detail] = str.split(",");
data[code] = detail;
});
break;
case "kiln-info":
for (const key in template.kilnInfo) {
data[key] = utils.getRandom(template.kilnInfo[key]);