led-bot/lights/request.js

23 lines
465 B
JavaScript
Raw Normal View History

2020-11-09 04:15:14 +00:00
import * as net from 'net';
2020-11-08 03:29:13 +00:00
2020-11-09 04:15:14 +00:00
const hostname = '192.168.1.219';
const port = 29999;
function getBinarySize(string) {
return Buffer.byteLength(string, 'utf8');
}
2020-11-08 03:29:13 +00:00
export default function req(data, callback, errorCallback) {
2020-11-09 04:15:14 +00:00
let string_data = JSON.stringify(data);
let size = getBinarySize(string_data);
let client = new net.Socket();
client.connect(port, hostname, () => {
let res = client.write(size);
if (res) {
client.write(string_data);
}
});
2020-11-08 03:29:13 +00:00
}