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