ami-io
npmパッケージの機能をオーバーロード/置換しようとしています。このパッケージは、ソケットインタフェースであるアスタリスクAMIと通信するために作成されています。公開npmパッケージの一部の機能を変更しました
ほとんど同じインターフェイスを持つサービスと話す必要がありますが、ログイン時に別の挨拶文字列が表示され、ログオンに余分なフィールドが必要です。すべての残りは同じです。 600 LOC ami-io
パッケージを単純にコピーし、2行または3行を変更する代わりに、挨拶文字列とログイン機能を検出する関数をオーバーライドして、ami-io
パッケージを使用し続けます。
次の関数が含まれているファイルindex.js
ありami-io
パッケージ内側:
Client.prototype.auth = function (data) {
this.logger.debug('First message:', data);
if (data.match(/Asterisk Call Manager/)) {
this._setVersion(data);
this.socket.on('data', function (data) {
this.splitMessages(data);
}.bind(this));
this.send(new Action.Login(this.config.login, this.config.password), function (error, response) {
if (response && response.response === 'Success') this.emit('connected');
else this.emit('incorrectLogin');
}.bind(this));
} else {
this.emit('incorrectServer', data);
}
};
は今、私はAsterisk Call Manager
にない一致させたいが、MyService
に、私は別のものでAction.LoginExt(this.config.login, this.config.password)
を定義し、使用したいが、余分なパラメータで
これは可能ですか?私は自分のモジュールでこれを試してみました:
var AmiIo = require('ami-io');
var amiio = AmiIo.createClient({port:5038, host:'x.x.x.x', login:'system', password:'admin'});
amiio.prototype.auth = function (data) {
this.logger.debug('First message:', data);
if (data.match(/MyService Version/)) {
this._setVersion(data);
this.socket.on('data', function (data) {
this.splitMessages(data);
}.bind(this));
this.send(new Action.LoginExt(this.config.login, this.config.password, this.config.extra), function (error, response) {
if (response && response.response === 'Success') this.emit('connected');
else this.emit('incorrectLogin');
}.bind(this));
} else {
this.emit('incorrectServer', data);
}
};
...しかし、それはTypeError: Cannot set property 'auth' of undefined
が生じ、そして今私は無知です。 また、自分のモジュールに新しいAction.LoginExt
オブジェクトを定義できますか?どうやって?
次のようにaction.jsモジュールはアクションオブジェクトを定義しています
function Action(name) {
Action.super_.bind(this)();
this.id = this.getId();
this.set('ActionID', this.id);
this.set('Action', name);
}
(function(){
var Message = require('./message.js');
var util = require('util');
util.inherits(Action, Message);
})();
Action.prototype.getId = (function() {
var id = 0;
return function() {
return ++id;
}
})();
function Login(username, secret) {
Login.super_.bind(this, 'Login')();
this.set('Username', username);
this.set('Secret', secret);
}
... more functions ...
(function() {
var actions = [
Login,
... more functions ...
];
var util = require('util');
for (var i = 0; i < actions.length; i++) {
util.inherits(actions[i], Action);
exports[actions[i].name] = actions[i];
}
exports.Action = Action;
})();
私は理解してどう思うかはアクションがメッセージからサブクラス化されていることです。その順番でLogin関数はActionからサブクラス化され、エクスポートされます(最後のコードブロックで)。
// extend ami-io with LoginExt function
function LoginExt(username, secret, company) {
Login.super_.bind(this, 'LoginExt')();
this.set('Username', username);
this.set('Secret', secret);
this.set('Company', company);
}
var util = require('util');
util.inherits(LoginExt, amiio.Action);
しかしutil.inheritsが未定義で失敗します。 は、だから私は私のコードでは、私は似た何かを試みることができると思います。私はami-ioにも問題を開いた。
ありがとうございました。それはとても簡単です。私は今、アクションを追加/拡張することで取り組んでいます。実際には、モジュール自体を変更する方が簡単かもしれませんが、技術的負債を生み出す私自身のコードベースに取り込む必要があります。または、forkして名前を変更し、npm repoに入れます。ほんの数行のコードの場合 – raarts