在做添加好友之间,必须要对xmpp的好友订阅有一定的了解,以前我不了解其中原理,盲目的做,以为添加到了各自的列表能够发消息就没事了,后来发现这样会导致很多问题,比如好友上下线无提醒,好友更新了vcard没有提示等一系列和好友相关的问题都出来了。
?
后来终于重视这个问题,查找相关资料,自行百度,发现很多资料都是只说了单边实现没有设计原理,事实上,那也不算真的实现了,现在我来说下原理和流程,希望对同是做即时通讯的同学们有所帮助;
?
我们知道,一个好友就对应一个RosterEntry,那我们看怎么来构造一个RosterEntry对象:
?
?
/** * Creates a new roster entry. * * @param user the user. 用户的jid * @param name the nickname for the entry. 指定给这个用户的昵称(其实我更愿意叫做备注) * @param type the subscription type. 这个用户和我的好友关系类型 * @param status the subscription status (related to subscriptions pending to be approbed).这个用户和我的好友状态 * @param connection a connection to the XMPP server.连接,不解释 */ RosterEntry(String user, String name, RosterPacket.ItemType type, RosterPacket.ItemStatus status, Roster roster, Connection connection) { this.user = user; this.name = name; this.type = type; this.status = status; this.roster = roster; this.connection = connection; }
?
?
?
上面的user和nickname还有Connection相信大家都知道了,Roster是花名册,相信大家也不陌生,通过XmppConnection.getRoster()可以获取到当前连接的花名册,我们重点来说下type和?status是干嘛用的;
?
凭我们用过qq的经验来看,添加好友是需要有几种状态的,比如我加了你,你同意了,但是你不想把动态让我看到,所以你不一定加我,这就是单方订阅,我加你,但是你拒绝了我 ,那我们就没啥关系,我加了你,你也加了我,那我们就是朋友了,我们是双方订阅,现在我们来捋一捋xmpp中有哪几种状态;
?
我们先看itemType是个什么鬼,点进去源码看一下;
?
?
public static enum ItemType { /** * The user and subscriber have no interest in each other‘s presence. */ none, /** * The user is interested in receiving presence updates from the subscriber. */ to, /** * The subscriber is interested in receiving presence updates from the user. */ from, /** * The user and subscriber have a mutual interest in each other‘s presence. */ both, /** * The user wishes to stop receiving presence updates from the subscriber. */ remove }
?
?
我们看到,这是一个枚举类型,上面都有注释,为了大家更清楚一些我就给大家翻译一下;none表示我和对方没有任何关系;to表示我发了我请求订阅了对方,对方同意了,但是他没有订阅我;from就是to反过来,他订阅了我,但是我没有订阅他;both表示我们双方互相订阅了;remove表示我想取消以前的订阅;
?
这种订阅状态是通过发送Presence来实现的。我们结合具体的应用场景来讲一下,怎么来订阅:
?
?
一、添加者 1、加入到 用户 roster列表 <iq type="set"><query xmlns="jabber:iq:roster"><item jid="13548583222@iz28sr0uiyaz" name="13548583222"></item></query></iq> 2、发送订阅presence <presence type="subscribe" to="13548583222@iz28sr0uiyaz"></presence> 3、对方同意,互相订阅 <presence type="subscribed" to="13548583222@iz28sr0uiyaz"></presence> 二、被添加者 接受好友添加 1 、发送接受请求订阅 <presence type="subscribed" to="13548583222@iz28sr0uiyaz"></presence> 2、添加到roster列表 <iq type="set"><query xmlns="jabber:iq:roster"><item jid="13548583222@iz28sr0uiyaz"></item></query></iq> 3、发送订阅 <presence type="subscribe" to="13548583222@iz28sr0uiyaz"></presence> 这时添加者发现已经在花名册中了,只需要发送subscribed就OK了,同时更新自己显示的列表; 添加者:<presence type="subscribed" to="13548583222@iz28sr0uiyaz"></presence>
?
?
上面这个流程通过发包的方式呈现,可能大家不是很明白,那么我画一个UML图加深理解;
?
?到此,两个人就成为了双方的好友了,这时候只要有一方有更新,另一方就会受到与之相关的消息,比如vcard更新,状态更新等;
?
那么既然加了好友,我们来看下好友列表这块,如果我们只想显示双方都添加对方好友的列表,那我们需要做一个刷选,回归到前面的RosterEntry来,做一个判断:
?
if ( entry.getType()==ItemType.both) { User user = transEntryToUser(entry); tempUsers.add(user); UserManager.getInstance(mContext).saveUserDetail2Local(user); }
?这样的话,好友列表就只有互相都订阅的好友了;
?
本文属原创,如需转载请注明出处,尊重劳动成果,谢谢!
有问题欢迎下面留言问题;
?
原文:http://zhonglunshun.iteye.com/blog/2252691