I wrote a TCP client and server in node.js. I can't figure out how to make client reconnect when it disconnects for any reason whatsoever. I understand I have to handle 'close' event, but it's an event that gets fired only when connection gracefully closes. I don't know how to handle network errors when connection just ends because of network issues.
Here's the code that I've so far:
function connect () {
client = new net.Socket();
client.connect(config.port, config.host, function () {
console.log('connected to server');
});
client.on('close', function () {
console.log('dis-connected from server');
connect();
});
client.on('error', ...); // what do I do with this?
client.on('timeout', ...); // what is this event? I don't understand
}
Can anyone explain what do I do in error and timeout case? And how do I know that the network has disconnected and reconnect?