Skip to content

Instantly share code, notes, and snippets.

@nandenjin
Last active July 18, 2024 08:28
Show Gist options
  • Save nandenjin/9133af1145bbfea9c1d6f3b7dce4b009 to your computer and use it in GitHub Desktop.
Save nandenjin/9133af1145bbfea9c1d6f3b7dce4b009 to your computer and use it in GitHub Desktop.
Rewrited artnet-node
const Server = require("./server");
Server.listen(6454, function (...arg) {
console.log(arg);
});
import util from "util";
import events from "events";
import dgram from "dgram";
// ArtNet server class
const listen = function (port, cb) {
this.port = port;
events.EventEmitter.call(this);
// Set up the socket
const sock = dgram.createSocket("udp4", function (msg, peer) {
const data = new Array();
for (let i = 0; i < msg.length; i++) {
let d = msg.toString().charCodeAt(i);
// Since we can't do unsigned 8-bit integers, do some normalization
if (d < 0) {
d = 0;
} else if (d > 255) {
d = 255;
}
// Append the byte to the array
data.push(d);
}
// Deseralize the data - magic numbers are as per the Art-Net protocol
const sequence = data[12];
const physical = data[13];
const universe = data[14] * 256 + data[15];
const length = data[16] * 256 + data[17];
const rawData = new Array();
for (let i = 0; i < length; i++) {
rawData.push(data[i + 18]);
}
// Build the associative array to return
const retData = {
sequence: sequence,
physical: physical,
universe: universe,
length: length,
data: rawData,
};
// And call the callback passing the deseralized data
cb(retData, peer);
});
sock.bind(port);
};
// Setup EventEmitter for the ArtNetServer
util.inherits(listen, events.EventEmitter);
export { listen };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment