Skip to content

Instantly share code, notes, and snippets.

@image72
Created July 28, 2023 17:40
Show Gist options
  • Save image72/be8bb4be4109779cd65639d110815a9a to your computer and use it in GitHub Desktop.
Save image72/be8bb4be4109779cd65639d110815a9a to your computer and use it in GitHub Desktop.
auto mock response via request points.
#!/usr/bin/env node
// websocketd --port 8058 ./mocker.js
const querystring = require('querystring');
const qs = process.env?.QUERY_STRING.replace('QUERY_STRING=', '');
const qsObj = querystring.parse(qs);
process.stdin.resume();
process.stdin.setEncoding('utf8');
// Receive data from WebSocket - STDIN
process.stdin.on('data', function (req) {
// Send data to WebSocket client - STDOUT
const data = JSON.parse(req.trim());
startInterval(() => {
const values = data.points.map(item => {
const value = item.includes('_YX') ? Math.round(Math.random()) : Math.random() * 100;
return { point: item, value };
});
process.stdout.write(JSON.stringify({ now: +new Date(), values, type: "GetNowValue" }) + '\n');
}, 5000);
})
function startInterval(callback, ms) {
callback();
return setInterval(callback, ms);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment