xuchang-screen/websocket/server.ts

126 lines
3.0 KiB
TypeScript
Raw Normal View History

2023-09-10 15:09:12 +08:00
import { WebSocket, WebSocketServer } from 'ws';
2023-09-11 13:28:34 +08:00
import template from './template.json';
2023-09-10 20:35:29 +08:00
import utils from './utils';
2023-09-10 15:09:12 +08:00
const wss = new WebSocketServer({ port: 9800 });
2023-09-10 21:44:05 +08:00
const frequency = 10; // seconds
2023-09-10 15:09:12 +08:00
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(() => {
2023-09-10 20:35:29 +08:00
sendMsg(ws, 'kiln-info'); // 窑炉信息
2023-09-10 21:44:05 +08:00
sendMsg(ws, 'run-state'); // 运行状态
sendMsg(ws, 'energy-cost'); // 运行状态
2023-09-11 10:39:38 +08:00
sendMsg(ws, 'realtime');
sendMsg(ws, 'his-trend');
2023-09-10 21:44:05 +08:00
// sendMsg(ws, 'gas');
// sendMsg(ws, 'kiln-top');
// sendMsg(ws, 'kiln-bottom');
2023-09-10 15:09:12 +08:00
}, frequency * 1000);
ws.on('close', function () {
console.log('停止监听');
clearInterval(timer);
});
});
2023-09-10 21:44:05 +08:00
type MsgType =
| 'kiln-info'
| 'run-state'
| 'energy-cost'
2023-09-11 10:39:38 +08:00
| 'realtime'
| 'his-trend'
2023-09-10 21:44:05 +08:00
| 'fan'
| 'gas'
| 'kiln-top'
| 'kiln-bottom';
2023-09-10 15:09:12 +08:00
2023-09-11 10:39:38 +08:00
type ResponseData = {
[key: string]: string | string[];
};
type ResponseDataComplex = {
[key: string]: ResponseData;
};
2023-09-10 15:09:12 +08:00
function sendMsg(ws: WebSocket, type: MsgType) {
2023-09-11 10:39:38 +08:00
let data: ResponseData | ResponseDataComplex = {};
2023-09-10 15:09:12 +08:00
switch (type) {
2023-09-10 20:35:29 +08:00
case 'kiln-info':
2023-09-11 13:28:34 +08:00
for (const key in template.kilnInfo) {
2023-09-10 20:35:29 +08:00
data[key] = utils.getRandom(
2023-09-11 13:28:34 +08:00
template.kilnInfo[key as keyof typeof template.kilnInfo],
2023-09-10 20:35:29 +08:00
);
}
2023-09-10 15:09:12 +08:00
break;
2023-09-10 21:44:05 +08:00
case 'energy-cost':
2023-09-11 13:28:34 +08:00
for (const key in template.energyCost) {
2023-09-10 21:44:05 +08:00
data[key] = utils.getRandom(
2023-09-11 13:28:34 +08:00
template.energyCost[key as keyof typeof template.energyCost],
2023-09-10 21:44:05 +08:00
);
}
break;
case 'run-state':
2023-09-11 13:28:34 +08:00
data = template.runState;
2023-09-10 21:44:05 +08:00
break;
2023-09-11 10:39:38 +08:00
case 'realtime':
/** 天然气 实时流量 */
2023-09-11 13:28:34 +08:00
data.gasii = template.realtime.gasii.map((v) => utils.getRandom(v));
2023-09-11 10:39:38 +08:00
/** 助燃风 实时流量 */
2023-09-11 13:28:34 +08:00
data.wind = template.realtime.wind.map((v) => utils.getRandom(v));
2023-09-11 10:39:38 +08:00
break;
case 'his-trend':
const hisTrend: {
gas: ResponseData;
wind: ResponseData;
} = {
gas: {},
wind: {},
};
2023-09-11 13:28:34 +08:00
Object.keys(template.hisTrend.gas).forEach((key) => {
hisTrend.gas[key] = template.hisTrend.gas[
key as keyof typeof template.hisTrend.gas
2023-09-11 10:39:38 +08:00
].map((v) => utils.getRandom(v));
});
2023-09-11 13:28:34 +08:00
Object.keys(template.hisTrend.wind).forEach((key) => {
hisTrend.wind[key] = template.hisTrend.wind[
key as keyof typeof template.hisTrend.wind
2023-09-11 10:39:38 +08:00
].map((v) => utils.getRandom(v));
});
data = hisTrend;
2023-09-10 15:09:12 +08:00
break;
case 'gas':
2023-09-11 13:28:34 +08:00
// data = template.gas;
2023-09-10 15:09:12 +08:00
break;
case 'kiln-top':
2023-09-11 13:28:34 +08:00
// data = template.kilnTop;
2023-09-10 15:09:12 +08:00
break;
case 'kiln-bottom':
2023-09-11 13:28:34 +08:00
// data = template.kilnBottom;
2023-09-10 15:09:12 +08:00
break;
default:
2023-09-10 20:35:29 +08:00
// data = 'You are connected!';
2023-09-10 15:09:12 +08:00
break;
}
// console.log("sendMsg: ", ws);
// ws.emit("message", JSON.stringify(data));
2023-09-10 20:35:29 +08:00
ws.send(JSON.stringify({ type, data }));
2023-09-10 15:09:12 +08:00
}