Skip to content

Instantly share code, notes, and snippets.

@torabian
Last active April 14, 2020 15:22
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 torabian/bf3d6325eb78200a5ae4edbd74c93f5b to your computer and use it in GitHub Desktop.
Save torabian/bf3d6325eb78200a5ae4edbd74c93f5b to your computer and use it in GitHub Desktop.
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