Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to try catch ws error "getaddrinfo ENOTFOUND"?

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.

like image 663
Jerrylk Avatar asked Oct 25 '25 21:10

Jerrylk


1 Answers

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
}
like image 69
Waqas Ahmed Avatar answered Oct 27 '25 12:10

Waqas Ahmed