englercj / node-esl

FreeSWITCH ESL implementation for Node.js; implements the full Event Socket Library specified in: http://wiki.freeswitch.org/wiki/Esl
http://englercj.github.com/node-esl/
MIT License
171 stars 110 forks source link

Reconnect esl Connection if connection drops #34

Open peili opened 10 years ago

peili commented 10 years ago

In case FS gets restarted I also need to create new esl conection on node. Have you implemented a function which tries to reconnect in an interval in case connection to fs drops?

englercj commented 10 years ago

No I haven't, but it should be pretty easy to do with the esl::end (emitted when connection to fsw is lost) or esl::event::disconnect::notice (advisory event from fsw) events and a setTimeout loop that tries to reconnect on an interval.

Something similar to:

var conn = null,
    reconnectTimeout = null,
    waitTime = 1000; //wait 1 sec between tries

function doConnect() {
    //as of today there is no "connect" method, so you have to create a new object.
    conn  = new Connection();

    //if there is an error, wait some time then try again
    conn.on('error', function(err) {
        //will need to check if this error is a connection error here, otherwise any error will trigger a new connection.
        setTimeout(doConnect, waitTime);
    });
}

doConnect();

This can be a little cleaner if I move the connection logic to a method that you can just call repeatedly, instead of creating a new object each time. Also it would be nice to have retry built in. I'll mark this as a feature request and get to it sometime if I can (also accepting pull requests).