Skip to content

Commit

Permalink
Fix AMI Re-connection (#62)
Browse files Browse the repository at this point in the history
* Change reconnection times

The old reconnection metodology was increasing the reconnection time almost for a day, that was to much, so, now the default reconnection was change to 10 seconds. For each reconnection try, the time is incresed 10 seconds, the reconnection may reach a maximum of 60 seconds.

* Missed parameter when reconnecting

The connect function was expecting 4 parameters, but only 3 parameters were sending, due this, the host parameter was containing invalid value, avoiding to recconect to Asterisk AMI.

* Remove Node Reference

Due the author of this plugin doesn't update the node repo, I think is not necessary to show the reference to node PKG, actually the node code is old and have not been updated for awhile.
  • Loading branch information
eagle26 authored Jun 15, 2021
1 parent 037a758 commit c3b4fb5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Asterisk Manager API [![Total views](https://sourcegraph.com/api/repos/github.com/pipobscure/NodeJS-AsteriskManager/.counters/views.png)](https://sourcegraph.com/github.com/pipobscure/NodeJS-AsteriskManager)
[![NPM](https://nodei.co/npm/asterisk-manager.png)](https://nodei.co/npm/asterisk-manager/)
# Asterisk Manager API

For a project of mine I needed a low level interface to the Asterisk Manager API. I looked around and found https://github.com/mscdex/node-asterisk . While it was a good starting point, it had too many abstractions for my taste. Which is why I based my version on it an then radically refactored it. In the end there now is very little in common with it.

Expand Down
14 changes: 9 additions & 5 deletions lib/ami.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var Utils = require('./utils');
var Manager = function Manager(port, host, username, password, events) {

var obj = {};
var context = { backoff: 10 };
var context = { backoff: 10000 };
var properties = ['on', 'once', 'addListener', 'removeListener', 'removeAllListeners',
'listeners', 'setMaxListeners', 'emit'];

Expand Down Expand Up @@ -235,12 +235,16 @@ function ManagerKeepConnected(context) {
}
}
function ManagerReconnect(context) {
var connect = this.connect.bind(this.options.port, this.options.host, this.login.bind(this));
setTimeout(connect, context.backoff);
context.backoff *= context.backoff;
console.log('Trying to reconnect to AMI in '+ (context.backoff / 1000) +' seconds');

var connect = this.connect.bind(context, this.options.port, this.options.host, this.login.bind(this));
setTimeout(connect, context.backoff);
if(context.backoff < 60000){ //The maximum reconection time is 60 seconds
context.backoff += 10000; //Increase reconnection time by 10 seconds
}
}
function ManagerResetBackoff(context) {
context.backoff = 10;
context.backoff = 10000;
}

function MakeManagerAction(req, id) {
Expand Down

0 comments on commit c3b4fb5

Please sign in to comment.