Что то типа такого должно получиться?
const net = require('net');
const HOST = '172.16.221.106';
const PORT = 14074;
const client = new net.Socket();
let socketData = '';
let socketWaitHello = true;
client.connect(PORT, HOST);
client.on('connect', () => {
console.log(`CONNECTED TO: ${HOST}:${PORT}`);
client.write(`<proto ver="1.0"/>`);
socketWaitHello = true;
});
client.on('data', (data) => {
socketData += data.toString();
});
client.on('end', () => {
console.log(`DATA: ${socketData}`);
if (socketWaitHello) {
if (socketData === `<proto ver="1.0">OK</proto>`) {
client.write(`<some_auth_data />`);
}
else if (socketData === `<auth_is_accepted />`) {
socketWaitHello = false;
client.write(`<get_some_data />`);
}
else {
throw new Error(`Server don't answered for hello`);
}
}
else {
do_something_with_data(socketData);
}
socketData = '';
});
client.on('close', () => {
console.log(`Connection closed`);
});