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 'closed' event, but it is an incident that only gets removed when the connection is closed gracefully

here So far the code I have made is:

  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}   

Can anyone explain what I can do in the error and timeout ? And how do I know that the network has disconnected and reconnected?

You may only see 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.

In addition, event handlers pass near a boolean hariser argument indicates that the socket was due to a closed error and not a normal connection.

The incident is set to be used to determine the loss of traffic between two endpoints (i.e. between the middle.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -