【问题标题】:How to get response from node-xmpp request?如何从 node-xmpp 请求中获得响应?
【发布时间】:2023-03-27 21:31:01
【问题描述】:

我学会了使用 node-xmpp 库向 XMPPserver 发出请求。现在我可以提出 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 库获取我们发出的请求的回调结果

【问题讨论】:

    标签: javascript node.js websocket xmpp ejabberd


    【解决方案1】:

    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();
    });
    

    【讨论】:

      【解决方案2】:

      你可以从连接中获取原始数据

      cl.connection.on("data", function (data) {
          console.log('data', data.toString('utf8'));
      });
      

      【讨论】:

        猜你喜欢
        • 2020-12-27
        • 1970-01-01
        • 2013-04-02
        • 2017-07-29
        • 1970-01-01
        • 2017-06-22
        • 1970-01-01
        • 1970-01-01
        • 2017-01-20
        相关资源
        最近更新 更多