Created
July 28, 2023 17:40
-
-
Save image72/be8bb4be4109779cd65639d110815a9a to your computer and use it in GitHub Desktop.
auto mock response via request points.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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