Last active
April 14, 2020 15:22
-
-
Save torabian/bf3d6325eb78200a5ae4edbd74c93f5b to your computer and use it in GitHub Desktop.
This file contains 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
const dgram = require("dgram"); | |
const sample = { | |
// The device model (unique id, or something that makes the device identical) | |
model: "virtual", | |
// Id of the device in the dashboard | |
id: "temperature_device_1", | |
// Device version. | |
ver: "1.0.0", | |
// Type of the event | |
type: "stchng", | |
// Data that you want to send to the dashboard. | |
data: { | |
t1: 0, | |
}, | |
}; | |
function SendUDPMessage(msg, port = 6364, host) { | |
const PORT = 6364; | |
const pack = JSON.stringify(msg); | |
const message = Buffer.from(pack); | |
const client = dgram.createSocket("udp4"); | |
return new Promise((resolve, reject) => { | |
client.send(message, 0, message.length, PORT, host, function (err, bytes) { | |
if (err) { | |
return reject(err); | |
} | |
client.close(); | |
resolve(bytes); | |
}); | |
}); | |
} | |
SendUDPMessage(sample, 6364, "demo.ovio.ir") | |
.then((result) => { | |
console.log(result); | |
}) | |
.catch((error) => { | |
console.error(errror); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment