Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strophe.Connection.addHandler no works if call Strophe.Connection.sendIQ

I have a question about the Strophe.Connection.addHandler and Strophe.Connection.sendIQ. The code below can works:

Strophe.Connection.addHandler(context.onMessage, null, 'message')

onMessage = function (msgXML){
    var to = msgXML.getAttribute('to');
    var from = msgXML.getAttribute('from');
    var fromBareJid = Strophe.getBareJidFromJid(from);
    var type = msgXML.getAttribute('type');
    var elems = msgXML.getElementsByTagName('body');
    var body = elems[0]
    var text = Strophe.getText(body);
    return true;
}

But if I call setContactData after call addHandler,the method onMessage will never be call back,but the method onRoster can be call after sendIQ.

setContactData = function(){
    var iq = $iq({
        type: 'get'
    }).c('query', {
        xmlns: 'jabber:iq:roster'
    });
    Strophe.Connection.sendIQ(iq, context.onRoster);
}

onRoster = function(iq){
    $(iq).find('item').each(function(){
        var jid = $(this).attr('jid');     
        // transform jid into an id
        var jid_id = CommonUtil.getNameFromJid(jid);
        userList = userList + "','" + jid_id;
    });

Why can not call back the onMessage? What is the root cause? What I am missing?

like image 754
Jian Huang Avatar asked Oct 06 '22 03:10

Jian Huang


1 Answers

try this:

addHandler(context.onMessage, null, 'message', 'chat')//or 'normal'


from Wrox Professional XMPP Programming with JavaScript and jQuery Book:
The addHandler() function takes one or more parameters. The first parameter is the function that is invoked when a matching stanza is received. The rest of the parameters are matching criteria.
The full list of these parameters is shown in this abbreviated function definition from the Strophe source code:

addHandler: function (handler, ns, name, type, id, from) {
// implementation omitted
}

If any of the criteria are null or undefined, any stanza will match. Otherwise, stanzas will match only if they satisfy the criteria by string equality in a particular part of the stanza.
The last four criteria — name, type, id, and from — specify filters on the stanza’s element name and the type, id, and from attributes. These four criteria are checked only on the top-level element, not on any of the element’s descendants. The first criterion, ns, is slightly different, and it is checked for the top-level element as well as its immediate children. You see why shortly. The name criterion will almost always be null, to match any stanza, or one of message, presence, or iq. The addHandler() example set up a handler that would be called for any stanza received.
The type, id, and from criteria match the main attributes of , and stanzas.
You can use type to differentiate between regular chat messages and group chat messages or to separate out IQ-result stanzas from IQ-error stanzas. The id criterion is often used to handle replies to specific requests, like the IQ-result associated with a particular IQ-get request.

like image 172
Abdoljabbar Avatar answered Oct 10 '22 03:10

Abdoljabbar