Skip to content

Instantly share code, notes, and snippets.

@cpq
Last active September 11, 2020 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpq/a89ba60a842ddbc2cac5414d16a74ff4 to your computer and use it in GitHub Desktop.
Save cpq/a89ba60a842ddbc2cac5414d16a74ff4 to your computer and use it in GitHub Desktop.
// Simulate mdash.net device. To run this script, install ws library first:
// $ npm -g i ws
// $ node devsim.js MDASH_DEVICE_TOKEN
const Websocket = require('ws'); // npm install -g ws
const pass = process.argv[2]; // Device password
const addr = 'wss://mdash.net/api/v2/rpc?access_token=' + pass;
const ws = new Websocket(addr, {origin: addr});
ws.on('error', msg => console.log(msg.toString()));
ws.on('message', msg => {
const obj = JSON.parse(msg.toString());
console.log('Got message:', obj);
if (obj.method == 'Sys.GetInfo') // Answer on Sys.GetInfo RPC call
ws.send(JSON.stringify({id: obj.id, result: {app: 'sim', arch: 'js'}}));
});
// Send a HTTP.Request notification
setTimeout(
ev => ws.send(JSON.stringify({
method: 'HTTP.Request',
params: {
url: 'http://HOST:PORT',
method: 'POST',
body: 'my data',
headers: {HDR1: 'custom header'}
}
})),
1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment