Skip to content

Instantly share code, notes, and snippets.

@robertklep
Created December 29, 2018 19:59
Show Gist options
  • Save robertklep/254795545456d973824c16a99797af78 to your computer and use it in GitHub Desktop.
Save robertklep/254795545456d973824c16a99797af78 to your computer and use it in GitHub Desktop.
const WebSocket = require('ws');
const ws = new WebSocket('ws://192.168.8.122:8081', [ 'chat' ]);
const APIKEY = 'e6ef18d4-07a2-47e8-9443-34e66c83f2ee';
const seq = () => String(Date.now());
let deviceId = null;
ws.on('open', function open(ev) {
ws.send(JSON.stringify({
"action": "userOnline",
"ts": String(0 | Date.now / 1000),
"version": 6,
"apikey": APIKEY,
"sequence": seq(),
"userAgent": "app"
}));
});
let first = true;
ws.on('message', function incoming(json) {
const data = JSON.parse(json);
console.log('response: %j', data);
if (! first) return;
first = false;
ws.send(JSON.stringify({
"action": "update",
"deviceid": data.deviceid,
"apikey": APIKEY,
"params": {
"switches": [
{
"outlet": 0,
"switch": "off"
}
]
},
"sequence": seq(),
"userAgent": "app"
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment