【问题标题】:Strophe js receive only one messageStrophe js 只收到一条消息
【发布时间】:2015-08-28 08:49:57
【问题描述】:

我将 strophe js 与 xmpp 服务器一起使用。我从 strophe 客户端向另一个客户端发送消息。客户端接收消息。从客户端我只能向 strophe 客户端发送一条消息。Strophe 客户端只接收一条消息。为什么?这里是我的代码:

    var connection = new Strophe.Connection('http://localhost:5280/http-bind/');

    connection.connect('marian112@localhost', 'secret', function (status)
        {
            if (status == Strophe.Status.CONNECTED)
            {   
                console.log('Connected to server')
                //connection.jid='marian12@localhost'
                connection.addHandler(on_chat_message, null, 'message', 'chat',null,null);
                connection.send($pres().c("priority").t("10"));
                connection.addHandler(on_presence, null, 'presence');
                connection.addHandler(on_error_iq, null, 'iq', 'error');
                connection.send($pres().tree());
                console.log(connection.jid);
                var sendMessageQuery = $msg({to: 'marian@localhost/BBXFRONT', type: 'chat'}).c('body').t('AAAAAA');
                connection.send(sendMessageQuery);
            }
        });
    var on_chat_message=function(msg) {
        var sendMessageQuery = $msg({to: 'marian@localhost/BBXFRONT', type: 'chat'}).c('body').t('bbbb');
        connection.send(sendMessageQuery);
    console.log(msg);
    console.log('You have received a message!');
    var to = msg.getAttribute('to');
    var from = msg.getAttribute('from');
    var type = msg.getAttribute('type');
    console.log(to+'  '+from+'   '+type);
    var elems = msg.getElementsByTagName('body');
    var message=Strophe.getText(body);
    console.log(message);
    return true;
}

var on_presence=function(stanza) {
    console.log(stanza);
    return true;
}

var on_error_iq=function(stanza) {
    console.log(stanza);
    return true;
}

【问题讨论】:

    标签: javascript strophe


    【解决方案1】:

    我知道这是一个旧线程,但我遇到了 XMPP 的问题,所以我偶然发现了你的代码。我已经检查过了,问题就在这里

    var elems = msg.getElementsByTagName('body');
    var message=Strophe.getText(body);
    

    您正在定义 elems,然后从 body 元素中获取文本,此时未定义 body 元素。由于 getElementsByTagName 返回一个数组,所以这段代码应该是这样的:

    var body = msg.getElementsByTagName('body');
    var message=Strophe.getText(body[0]);
    

    我知道它不再相关,但它可以帮助遇到 XMPP/Strophe 问题的人提供一个可行的示例。

    【讨论】:

      【解决方案2】:

      一切正常,但你可以像这样修改下面提到的行 connection.addHandler(on_chat_message, null, 'message', null,null,null);

      工作继续

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-02-10
        • 2019-02-23
        • 1970-01-01
        • 1970-01-01
        • 2023-03-16
        • 2023-03-31
        • 1970-01-01
        相关资源
        最近更新 更多