Strophe js只收到一条消息

时间:2015-08-28 08:49:58

标签: javascript strophe

我使用strophe js和xmpp服务器。我从strophe客户端发送消息到另一个client.Client接收消息.FromCLient我只能发送一条消息到strophe client.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;
}

2 个答案:

答案 0 :(得分:2)

我知道这是一个旧线程,但我遇到了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 问题的人有一个有效的例子。

答案 1 :(得分:-1)

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

其工作持续