leds/server.js

36 lines
848 B
JavaScript

import * as net from 'net';
const hostname = '0.0.0.0';
const port = 29999;
export function recv(callback, errorCallback) {
let server = new net.Server();
server.listen(port, hostname, () => {
server.on('connection', (con) => {
console.log('connection recieved: ' +
con.remoteAddress + ":" + con.remotePort);
con.on('data', (data) => {
callback(data);
});
con.on('close', () => {
console.log('recieved close for ' +
con.remoteAddress + ":" + con.remotePort);
con.destroy();
});
server.getConnections((err, cons) => {
if (err) {
console.error(err);
}
else {
console.log(`connections: ${cons}`);
}
})
})
server.on('error', (e) => {
server.close();
errorCallback(e);
});
});
}