3

I'm using nodejs using WebSocket "ws" to connect to another server B.

However, sometimes when the server B is not reachable, mainly "getaddrinfo ENOTFOUND" problem will stop my server, which is not accepted, I should use a try catch to handle the situation and try to connect later.

Where should I put the try catch? It won't trigger the ws.onerror callback.

I've found a similar situation in How to catch getaddrinfo ENOTFOUND

but I've no idea how to use that approach to the ws interface.

Community
  • 1
  • 1
Jerrylk
  • 379
  • 5
  • 19

1 Answers1

3

Try as below:

you can use domain which never down your server A

 var domain = require('domain');

 var d = domain.create();

 d.on('error', function(er) {        
    setTimeout(function() { createConnection() }, 60000); //create connection after 1 minute
});

d.run(function() {
    createConnection()
});


var createConnection = function() {
    //Create Connection with server B
}
Waqas Ahmed
  • 473
  • 5
  • 21