import { WebSocketServer } from 'ws'; const wss = new WebSocketServer({ port: 8080 }); const delay = ms => new Promise(resolve => setTimeout(resolve, ms)) wss.on('connection', function connection(ws) { console.log("Connection made") ws.on('message', async function message(data) { data = JSON.parse(data.toString()) console.log("Message received", data.toString()) if(data.topic == "start_test"){ for (var i = 0; i <= 10; i++) { await delay(1000); ws.send( JSON.stringify( { "topic": "data", "data" : { "ts": Date.now(), "temp_before_radiator": Math.random(), "temp_after_radiator": Math.random(), "pressure_before_radiator": Math.random(), "pressure_after_radiator": Math.random(), "wind_speed": Math.random() } } ) ); } ws.send(JSON.stringify({topic: "end_test"})); } }); });