zl程序教程

您现在的位置是:首页 >  其他

当前栏目

websocket 测试

2023-09-14 08:58:28 时间

npm install -g wscat

wscat -l 8888

wscat -c ws://127.0.0.1:8888 

 

const WebSocket = require('ws');

const ws = new WebSocket.Server({port: 8777});

ws.on('connection', function connection(ws) {
  setInterval(() => {
    const random = Math.floor(Math.random() * Math.floor(100));
    // 返回给 amis 的数据
    const data = {
      random
    };
    // 发送前需要转成字符串
    ws.send(JSON.stringify(data));
  }, 500);
});

npm i ws 

npm start