javascript - Strophe.Connection.addHandler no works if call Strophe.Connection.sendIQ - Stack Overflow

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

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?

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?

Share Improve this question edited Oct 31, 2012 at 9:07 Paolo Moretti 56.1k23 gold badges103 silver badges93 bronze badges asked Oct 31, 2012 at 8:43 Jian HuangJian Huang 531 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745343012a4623415.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信