NodeJS Websocket how to reconnect when server restarts
I've used https://github.com/joewalnes/reconnecting-websocket/blob/master/reconnecting-websocket.js with success.
You should be able to do:
ws = new ReconnectingWebSocket('ws://....');
ws.reconnectInterval = 60000; // try to reconnect after 10 seconds
Try this:
var reconnectInterval = x * 1000 * 60;
var ws;
var connect = function(){
ws = new WebSocket('ws://localhost');
ws.on('open', function() {
console.log('socket open');
});
ws.on('error', function() {
console.log('socket error');
});
ws.on('close', function() {
console.log('socket close');
setTimeout(connect, reconnectInterval);
});
};
connect();
You get to use the original implementation without having to wrap it.