How do I make a node.js TCP connection reconnect automatically? -
I wrote a TCP client and server in node.js. I can not understand that the way to reconnect the client is disconnected for any reason. I understand that I have to handle the here So far the code I have made is: Can anyone explain what I can do in the You may only see In addition, event handlers pass near a boolean The incident is set to be used to determine the loss of traffic between two endpoints (i.e. between the middle. 'closed' event, but it is an incident that only gets removed when the connection is closed gracefully
function connect () {client = new net. Socket (); Client.connect (config.port, config.host, function () {console.log ('connect to server');}); Client.on ('close', function () {console.log ('linked to server') '; connect ();}); client.on ('error', ...); // What do I do with this? Client.on ('timeout', ...); // What is this event? I do not understand}
error and
timeout ? And how do I know that the network has disconnected and reconnected?
error event handler for no-op / blank function In the form, even after
error ,
close event for TCP sockets is always emitted, you ignore it unless you want to log error or anything useful You can.
hariser argument indicates that the socket was due to a closed error and not a normal connection.
Comments
Post a Comment