我对 Strophe.Connection.addHandler 和 Strophe.Connection.sendIQ 有疑问。下面的代码可以工作:
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;
}
但是如果我在调用addHandler之后调用setContactData,onMessage方法永远不会被回调,但是onRoster方法可以在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;
});
为什么不能回调onMessage?根本原因是什么?我错过了什么?