我正在尝试重载/替换 ami-io
npm 包中的函数。创建该包是为了与 asterisk AMI(一个套接字接口(interface))通信。
我需要与具有几乎完全相同的界面的服务进行通信,但它在登录时显示不同的问候语字符串,并且在登录时需要一个额外的字段。其余的都一样。我不想简单地复制 600 LOC ami-io
包并修改两三行,而是想覆盖检测问候语字符串的函数和登录函数,并继续使用 ami-io
包。
在ami-io
包内有一个文件index.js
,其中包含以下函数:
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.登录,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 模块定义 Action 对象如下:
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;
})();
我认为我理解的是 Action 是 Message 的子类。 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 上提出了一个问题。
最佳答案
您可以使用:
var AmiIo = require('ami-io');
AmiIo.Action.Login = function NewConstructor(){}; //to override Login action
//new constructor shold extend AmiIo.Action.Action(actionName)
//and also, you can use
AmiIo.Action.SomeNewAction = function SomeNewAction(){};//to create new actuion
//it also should extend AmiIo.Action.Action(actionName);
AmiIo.Action 只是一个对象。所有构造函数都是它的字段。
要创建新事件,您不需要执行任何操作,因为它只是一个对象。如果服务器发送给您
Event: Armageddon
SomeField: 123
ami-io 将创建名为 'Armageddon'
的事件。
要重写 Client#auth() 方法,您应该这样做
var AmiIo = require('ami-io');
AmiIo.Client.prototype.auth = function (){};//new function
关于javascript - 修改公共(public) npm 包中的一些功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36411332/