leds/server.js

37 lines
848 B
JavaScript
Raw Normal View History

import * as net from 'net';
const hostname = '0.0.0.0';
2021-05-26 00:32:56 +00:00
const port = 29999;
2021-05-26 00:32:56 +00:00
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}`);
}
})
})
2021-05-26 00:32:56 +00:00
server.on('error', (e) => {
server.close();
errorCallback(e);
});
});
}