network stuff?

This commit is contained in:
jane 2020-11-08 23:15:14 -05:00
parent 6c7bbc2304
commit 2a6f2d6f9b
3 changed files with 42 additions and 16 deletions

View file

@ -1,18 +1,22 @@
import * as https from 'https';
import * as net from 'net';
const options = {
hostname: '192.168.1.219',
port: '29999',
path: '/',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
};
const hostname = '192.168.1.219';
const port = 29999;
function getBinarySize(string) {
return Buffer.byteLength(string, 'utf8');
}
export default function req(data, callback, errorCallback) {
const request = https.request(options, (res) => callback(res));
request.on('error', (error) => errorCallback(error));
request.write(JSON.stringify(data));
request.end();
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);
}
});
}