zmarius81 / asterisk.io

node.js asterisk pbx io
40 stars 25 forks source link

E_AMI_SOCKED_CLOSE: Lost connection to server. #4

Open thdsi opened 7 years ago

thdsi commented 7 years ago

I code the asterisk.io to listening events and/or send actions. But always after a time (random time) the node server crash with the follow error. Why? Thanks

This code throw error ami.on('error', function(err){ throw err; });

throw err; ^ E_AMI_SOCKED_CLOSE: Lost connection to server. at Socket. (/home/.../asterisk.io/lib/ami.js:272:30) at emitOne (events.js:77:13) at Socket.emit (events.js:169:7) at TCP._onclose (net.js:486:12)

zmarius81 commented 7 years ago

This error tell you that connection to asterisk ami (asterisk manager interface) is lost. Check if:

A simple solution is to do a reconnect.

ami.on('error', function(err){
       // if error is E_AMI_SOCKED_CLOSE call a reconnect function in N seconds
});
thdsi commented 7 years ago
ami.on('error', function (err) {
    console.log('err ' + err);
    if (err == 'E_AMI_SOCKED_CLOSE') {
        ami = aio.ami(ip, port, login, pass);
    }
});

Right? Thanks

thdsi commented 7 years ago

Does not work, when the connection fail, that code does not connect again.

zmarius81 commented 7 years ago

Hi aio.ami(...) return a new instance of AMI, so, previews ami variable will not have new events. You need to rebuild the new ami. Try a code like this

var aio = require('asterisk.io'),
    ami = null;

function asteriskConnectReconnect(){
     ami = aio.ami(/*config here*/); // @see: no var in front

     ami.on('error', function(err){
         // throw err;
        if(err == 'E_AMI_SOCKED_CLOSE' || err == 'E_AMI_SOCKED_ERROR'){
                   // E_AMI_SOCKED_CLOSE: lost connection to server
                   // E_AMI_SOCKED_ERROR: could not connect, maybe asterisk is down
                   setTimeout(function(){
                               asteriskConnectReconnect();
                   }, 1000); // try to connect/reconnect in 1 second
        }
    });

    am.on('eventAny', function(data){
    });
}

asteriskConnectReconnect();

I didn't test the code, hope is ok

ecmastermx commented 4 years ago

Good afternoon I did this configuration but it doesn't show anything in terminal, I don't know if you can help me in that var aio = require('asterisk.io'), ami = null; function connect(){ ami = aio.ami('10.168.0.10',5038,'admin','123456');

ami.on('error', function(err){
   if(err == 'E_AMI_SOCKED_CLOSE' || err == 'E_AMI_SOCKED_ERROR'){
              setTimeout(function(){
                connect();
              }, 1000); 
   }

}); ami.on('ready',function () {

ami.action('CoreSettings',{},function (data) {
    console.log('CoreSettings',data);

});

}); ami.on('eventStatus', function(data){ console.log(data); }); }

connect();

"Thanks for all"