大屏模板,增加自适应(包含websocket)

1、简单的Node服务端

const WebSocket = require('ws');

// 创建 WebSocket 服务器
const wss = new WebSocket.Server({ port: 8888 });

const getHeader = (protocol) => {
  const protocolArr = protocol.split(',')
  const headers = {};
  for (let i = 0; i < protocolArr.length; i += 2) {
    const key = protocolArr[i].trim();
    const value = protocolArr[i + 1].trim();
    headers[key] = value;
  }

  return headers;
}
let timer = null;
let num = 0;
let UID = '';

// 监听连接事件
wss.on('connection', (ws, req) => {
  const userName = req.url.replace('/', '');
  UID = userName;
  console.log('WebSocket connection established for URL:', userName);
  // 在 'upgrade' 事件处理程序中获取请求头信息
  const protocol = req.headers['sec-websocket-protocol'];
  console.log('Sec-WebSocket-Protocol:', getHeader(protocol)['X-Access-Token']);


  // 连接建立成功时触发
  console.log('Client connected');
  if(!timer) {
    const obj = [
      {
        name: 'SC001',
        value: 123
      },
      {
        name: 'SC001',
        value: 123
      }
    ]
    timer = setInterval(() => {
      ws.send(`${UID}心跳信息消息: ${num}--${JSON.stringify(obj)}`);
      num++
    }, 3000);
  }

  // 监听消息接收事件
  ws.on('message', (message) => {
    console.log(`Received message: ${message}`);

    // 向客户端发送消息
    ws.send(`服务端返回消息: ${message}`);
  });

  // 监听连接关闭事件
  ws.on('close', () => {
    // 连接关闭时触发
    console.log('Client disconnected');
    timer = null;
  });
});

2、大屏客户端





  
  
  大屏测试



  
  
大屏标题
API总数
19,612
调用数
1,222
告警数
68
222
333
111
222
333

你可能感兴趣的:(总结,websocket,网络协议,网络)