dmx
Version:
A nodejs DMX library
57 lines (43 loc) • 1.36 kB
JavaScript
const util = require('util');
const EventEmitter = require('events').EventEmitter;
function SocketioDriver(deviceId, options) {
options = options || {};
const self = this;
const { Server } = require("socket.io");
const port = options.port || 18909;
const debug = options.debug || false;
this.server = new Server(port);
this.server.on('connection', (socket) => {
if (debug) console.info(`Client connected [id=${socket.id}]`);
socket.on('disconnect', () => {
if (debug) console.info(`Client gone [id=${socket.id}]`);
});
});
this.universe = Buffer.alloc(513, 0);
self.start();
}
SocketioDriver.prototype.start = function () {};
SocketioDriver.prototype.stop = function () {
clearInterval(this.timeout);
};
SocketioDriver.prototype.close = cb => {
cb(null);
};
SocketioDriver.prototype.update = function (u, extraData) {
for (const c in u) {
this.universe[c] = u[c];
}
this.server.sockets.emit('update', [...this.universe]);
this.emit('update', u, extraData);
};
SocketioDriver.prototype.updateAll = function (v) {
for (let i = 1; i <= 512; i++) {
this.universe[i] = v;
}
this.server.sockets.emit('update', [...this.universe]);
};
SocketioDriver.prototype.get = function (c) {
return this.universe[c];
};
util.inherits(SocketioDriver, EventEmitter);
module.exports = SocketioDriver;