我学会了使用 node-xmpp 库向 XMPP 服务器发出请求。现在我可以按照 XMPP 扩展文档中所述提出请求。但现在我想获得每个请求的回调响应(尤其是 XML 响应)。
在这里,我使用了以下代码向另一个用户发出请求订阅(好友请求)
var net = require("net");
var xmpp = require('node-xmpp');
var cl = new xmpp.Client({ jid: "one@localhost", password: "comet123$" })
cl.addListener('online', function(data) {
console.log('Connected as ' + data.jid.user + '@' + data.jid.domain + '/' + data.jid.resource)
//making subscription
var stanza = new xmpp.Element('presence',{
to: "hai@localhost",
from: "one@localhost",
type: "subscribe",
}).up
// making request
cl.send(stanza);
// nodejs has nothing left to do and will exit
cl.end()
})
我想知道,如何得到响应结果。
我像这样尝试使用回调功能,
cl.send(stanza, function(result){
console.log(result);
});
也喜欢这个
var result = cl.send(stanza);
这只返回真,
那么谁能告诉我如何使用 node-xmpp 库获取我们发出的请求的回调结果
最佳答案
XMPP 消息没有回调或返回。您将不得不设置一个事件监听器来接收从服务器返回的消息。添加:
cl.on('stanza', function(stanza){
// Do something with the stanza
// If you want to end after the first message you get back, move this here
cl.end();
});
关于javascript - 如何从 node-xmpp 请求中获取响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23653295/