1
votes

I use strophe js with an xmpp server.I send from strophe client a message to another client.Client receive the message.FromCLient i can send only one message to strophe client.Strophe client receive only one message.Why?Here is my code:

    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

2 Answers

2
votes

I know this is an old thread, but I had an issue with XMPP, so I have stumbled upon your piece of code. I have checked it and the problem is here

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

You are defining elems and then getting text from body element, and body element is not defined at this point. Since getElementsByTagName returns an array, this chunk of code shoud be like this:

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

I know it is not relevant anymore, but it can help someone who have problems with XMPP/Strophe to have a working example.

-1
votes

Every thing ok but u can modify the below mention line like this connection.addHandler(on_chat_message, null, 'message', null,null,null);

its work contiue