重构xuchang-screen,从umi->cra, 计划引入redux
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

134 rindas
3.3 KiB

  1. import { WebSocket, WebSocketServer } from 'ws';
  2. import template from './template.json';
  3. import utils from './utils';
  4. const wss = new WebSocketServer({ port: 9800 });
  5. const frequency = 10; // seconds
  6. const frequency1 = 1200; // seconds
  7. wss.on('connection', function (ws, req) {
  8. // console.log("ws", ws);
  9. console.log(
  10. 'Client in: ',
  11. req.socket.remoteAddress,
  12. 'current users:',
  13. wss.clients.size,
  14. );
  15. // ws.on("error", console.error);
  16. // ws.emit("message", "connected");
  17. ws.on('open', function () {
  18. console.log('connected');
  19. ws.send('connected');
  20. });
  21. ws.on('message', function (msg) {
  22. console.log('message from client', msg);
  23. ws.send('echo ' + msg.toString());
  24. });
  25. ws.on('error', console.error);
  26. const timer = setInterval(() => {
  27. sendMsg(ws, 'kiln-info'); // 窑炉信息
  28. sendMsg(ws, 'energy-cost'); // 运行状态
  29. sendMsg(ws, 'run-state'); // 运行状态
  30. sendMsg(ws, 'realtime');
  31. sendMsg(ws, 'his-trend');
  32. // sendMsg(ws, 'gas');
  33. // sendMsg(ws, 'kiln-top');
  34. // sendMsg(ws, 'kiln-bottom');
  35. }, frequency * 1000);
  36. const timer1 = setInterval(() => {
  37. // sendMsg(ws, 'run-state'); // 运行状态
  38. }, frequency1 * 1000);
  39. ws.on('close', function () {
  40. console.log('停止监听');
  41. clearInterval(timer);
  42. clearInterval(timer1);
  43. });
  44. });
  45. type MsgType =
  46. | 'kiln-info'
  47. | 'run-state'
  48. | 'energy-cost'
  49. | 'realtime'
  50. | 'his-trend'
  51. | 'fan'
  52. | 'gas'
  53. | 'kiln-top'
  54. | 'kiln-bottom';
  55. type ResponseData = {
  56. [key: string]: string | string[];
  57. };
  58. type ResponseDataComplex = {
  59. [key: string]: ResponseData;
  60. };
  61. function sendMsg(ws: WebSocket, type: MsgType) {
  62. let data: ResponseData | ResponseDataComplex = {};
  63. switch (type) {
  64. case 'kiln-info':
  65. for (const key in template.kilnInfo) {
  66. data[key] = utils.getRandom(
  67. template.kilnInfo[key as keyof typeof template.kilnInfo],
  68. );
  69. }
  70. break;
  71. case 'energy-cost':
  72. for (const key in template.energyCost) {
  73. data[key] = utils.getRandom(
  74. template.energyCost[key as keyof typeof template.energyCost],
  75. );
  76. }
  77. break;
  78. case 'run-state':
  79. data = template.runState;
  80. data.fireDirection =
  81. Math.floor(Math.random() * 10) % 2 === 0 ? '南火' : '北火';
  82. break;
  83. case 'realtime':
  84. /** 天然气 实时流量 */
  85. data.gasii = template.realtime.gasii.map((v) => utils.getRandom(v));
  86. /** 助燃风 实时流量 */
  87. data.wind = template.realtime.wind.map((v) => utils.getRandom(v));
  88. break;
  89. case 'his-trend':
  90. const hisTrend: {
  91. gas: ResponseData;
  92. wind: ResponseData;
  93. } = {
  94. gas: {},
  95. wind: {},
  96. };
  97. Object.keys(template.hisTrend.gas).forEach((key) => {
  98. hisTrend.gas[key] = template.hisTrend.gas[
  99. key as keyof typeof template.hisTrend.gas
  100. ].map((v) => utils.getRandom(v));
  101. });
  102. Object.keys(template.hisTrend.wind).forEach((key) => {
  103. hisTrend.wind[key] = template.hisTrend.wind[
  104. key as keyof typeof template.hisTrend.wind
  105. ].map((v) => utils.getRandom(v));
  106. });
  107. data = hisTrend;
  108. break;
  109. case 'gas':
  110. // data = template.gas;
  111. break;
  112. case 'kiln-top':
  113. // data = template.kilnTop;
  114. break;
  115. case 'kiln-bottom':
  116. // data = template.kilnBottom;
  117. break;
  118. default:
  119. // data = 'You are connected!';
  120. break;
  121. }
  122. // console.log("sendMsg: ", ws);
  123. // ws.emit("message", JSON.stringify(data));
  124. ws.send(JSON.stringify({ type, data }));
  125. }