Javascript Strophe js只接收一条消息

Javascript Strophe js只接收一条消息,javascript,strophe,Javascript,Strophe,我在xmpp服务器上使用strophe js。我从strophe客户端向另一个客户端发送消息。客户端接收消息。从客户端我只能向strophe客户端发送一条消息。strophe客户端只接收一条消息。为什么?以下是我的代码: var connection = new Strophe.Connection('http://localhost:5280/http-bind/'); connection.connect('marian112@localhost', 'secret', f

我在xmpp服务器上使用strophe js。我从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;
}

每件事都可以,但是你可以像这样修改下面提到的行 connection.addHandler(在聊天信息上,null,'message',null,null,null)


它的工作继续

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

var elems = msg.getElementsByTagName('body');
var message=Strophe.getText(body);
您正在定义元素,然后从body元素获取文本,此时未定义body元素。由于getElementsByTagName返回一个数组,这段代码应该如下所示:

var body = msg.getElementsByTagName('body');
var message=Strophe.getText(body[0]);
我知道它不再相关了,但它可以帮助那些在XMPP/Strophe方面有问题的人提供一个工作示例