使用此Strophe命令时:
var iq = $iq({type: 'get'}).c('query', {xmlns: 'jabber:iq:roster'});
connection.sendIQ(iq)
我认为这是成功的回调:
<iq xmlns="jabber:client" xml:lang="pt-br" to="user01@localhost/100164477219111523302818" from="user01@localhost" type="result" id="82480785-c170-48d1-a180-bcadbff957d2:sendIQ">
<query xmlns="jabber:iq:roster">
<item subscription="both" jid="user02@localhost">
<group>Roster01</group>
<group>Roster02</group>
</item>
<item subscription="both" jid="admin@localhost">
<group>Roster01</group>
<group>Roster02</group>
</item>
<item subscription="both" jid="grupo02@conference.localhost">
<group>Roster02</group>
</item>
<item subscription="both" jid="grupo01@conference.localhost">
<group>Roster01</group>
</item>
</query>
</iq>
我想知道的是,是否有某种方法可以按群组及其成员对此回调进行分组。如果没有,我怎么能用Javascript来做。示例:
我使用ejabberd作为XMPP服务器,Ionic 3使用Strophe作为客户端。
答案 0 :(得分:2)
我建议使用strophejs-plugin-roster让事情变得更轻松:
// connect Strophe
connection = new Strophe.Connection(url);
connection.connect(my_jid, my_pwd, onConnect);
...
function onConnect(status) {
if (status == Strophe.Status.CONNECTED) {
...
// pass connection to roster plugin
connection.roster.init(connection);
}
}
这是一个通过插件获取getRoster的函数,result是一个包含对象的JS数组(而不是XML ...):
function getRoster() {
connection.roster.get(function (roster) {
console.log(' >roster:', roster);
for (var i in roster) {
console.log(' >buddy '+i+':');
console.log(' >'+roster[i].name+" ("+roster[i].jid+' -->'+roster[i].subscription);
console.log(' >', roster[i].groups);
}
// get buddies belonging to group1 and group2 (see below)
console.log(' >roster-group1:', getRosterGroup(roster, 'group1'));
console.log(' >roster-group2:', getRosterGroup(roster, 'group2'));
});
}
以下功能按群组过滤好友:
function getRosterGroup(roster, group) {
var reduced = roster.reduce(function(filtered, item) {
if (item.groups.indexOf(group)!==-1) {
filtered.push(item);
}
return filtered;
}, []);
return reduced;
}
这是一个有效的Plunker:http://plnkr.co/edit/XloJABSGHZvLTp3Js2KI?p=preview