The aim of this code is to reproduce the behavior of the Websocket server sending sensor information in real situation.
for information purpose, end devices used during the reccord was, respectively for torso, thighs, shins : 8, 4, 1
To do this, I recorded the messages received for a real case (with a dummy) which I store in a data variable stored in ./data.js in the form of a list containing :
{
"message" : "message in the form of an Object, sent by the server",
"time" : "time in ms that the server sends the corresponding message, in order to reproduce the sending times of each message."
}
// index.js
// install ws by running "npm install ws" after "npm init", lauch by running "node index.js"
const WebSocket = require('ws');
var wss
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const data = require("./data")
wss = new WebSocket.Server({ port: 7071 });
wss.on('connection', (ws) => {
sendws(ws)
})
async function sendws(ws) {
for (let i = 0; i < data.data.length; i++) {
ws.send(JSON.stringify(data.data[i].message))
if (i <= data.data.length-2) {
await sleep(data.data[i+1].time - data.data[i].time)
}
}
}
// data.js
const data = [
... // data from link
]