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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With