差别
这里会显示出您选择的修订版和当前版本之间的差别。
start:400webimintegration:contact_management [2016/12/08 03:21] liulj |
start:400webimintegration:contact_management [2018/09/05 09:54] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | ====== 好友管理 ====== | ||
- | ===== 查询好友列表 ===== | ||
- | |||
- | 查询好友列表时,要注意 subscription (both, to, from) 为不同值的处理。此处默认 both 和 to 的为好友,开发者自定义处理,保持跟 APP 端处理一致即可。 | ||
- | |||
- | <sxh javascript> | ||
- | conn.getRoster({ | ||
- | success: function ( roster ) { | ||
- | //获取好友列表,并进行好友列表渲染,roster格式为: | ||
- | /** [ | ||
- | { | ||
- | jid:'asemoemo#chatdemoui_test1@easemob.com', | ||
- | name:'test1', | ||
- | subscription: 'both' | ||
- | } | ||
- | ] | ||
- | */ | ||
- | for ( var i = 0, l = roster.length; i < l; i++ ) { | ||
- | var ros = roster[i]; | ||
- | //ros.subscription值为both/to为要显示的联系人,此处与APP需保持一致,才能保证两个客户端登录后的好友列表一致 | ||
- | if ( ros.subscription === 'both' || ros.subscription === 'to' ) { | ||
- | |||
- | } | ||
- | } | ||
- | }, | ||
- | }); | ||
- | </sxh> | ||
- | |||
- | ===== 添加/删除好友请求 ===== | ||
- | |||
- | 通过 SDK 的 subscribe 和 unsubscribe 进行添加或者删除好友操作,登录用户通过注册 onPresence,监听对方的添加或者删除好友请求,并做相应的处理。 | ||
- | |||
- | <sxh javascript> | ||
- | //easemobwebim-sdk中收到联系人订阅请求的处理方法,具体的type值所对应的值请参考xmpp协议规范 | ||
- | var handlePresence = function ( e ) { | ||
- | //(发送者希望订阅接收者的出席信息),即别人申请加你为好友 | ||
- | if ( e.type === 'subscribe' ) { | ||
- | //若e.status中含有[resp:true],则表示为对方同意好友后反向添加自己为好友的消息,demo中发现此类消息,默认同意操作,完成双方互为好友;如果不含有[resp:true],则表示为正常的对方请求添加自己为好友的申请消息。 | ||
- | } | ||
- | |||
- | //(发送者允许接收者接收他们的出席信息),即别人同意你加他为好友 | ||
- | if ( e.type === 'subscribed' ) { | ||
- | |||
- | } | ||
- | | ||
- | //(发送者取消订阅另一个实体的出席信息),即删除现有好友 | ||
- | if ( e.type === 'unsubscribe' ) { | ||
- | |||
- | } | ||
- | | ||
- | //(订阅者的请求被拒绝或以前的订阅被取消),即对方单向的删除了好友 | ||
- | if ( e.type === 'unsubscribed' ) { | ||
- | |||
- | } | ||
- | }; | ||
- | </sxh> | ||
- | |||
- | ===== 添加好友 ===== | ||
- | |||
- | |||
- | <sxh javascript> | ||
- | // 添加好友 | ||
- | var addFriends = function () { | ||
- | conn.subscribe({ | ||
- | to: 'username', | ||
- | // Demo里面接收方没有展现出来这个message,在status字段里面 | ||
- | message: '加个好友呗!' | ||
- | }); | ||
- | }; | ||
- | </sxh> | ||
- | |||
- | ===== 处理好友请求 ===== | ||
- | |||
- | 当收到“添加好友”的请求时,用户可能会同意或拒绝添加对方为好友,处理方式如下。 | ||
- | |||
- | ==== 同意添加对方为好友 ==== | ||
- | |||
- | <sxh javascript> | ||
- | conn.listen({ | ||
- | onPresence: function ( message ) { | ||
- | handlePresence(message); | ||
- | } | ||
- | }); | ||
- | |||
- | |||
- | //收到联系人订阅请求的处理方法,具体的type值所对应的值请参考xmpp协议规范 | ||
- | var handlePresence = function ( e ) { | ||
- | |||
- | //对方收到请求加为好友 | ||
- | if (e.type === 'subscribe') { | ||
- | | ||
- | /*同意添加好友操作的实现方法*/ | ||
- | conn.subscribed({ | ||
- | to: 'username', | ||
- | message : '[resp:true]' | ||
- | }); | ||
- | conn.subscribe({//需要反向添加对方好友 | ||
- | to: e.from, | ||
- | message : '[resp:true]' | ||
- | }); | ||
- | } | ||
- | } | ||
- | </sxh> | ||
- | |||
- | ==== 拒绝添加对方为好友 ==== | ||
- | |||
- | <sxh javascript> | ||
- | conn.listen({ | ||
- | onPresence: function ( message ) { | ||
- | handlePresence(message); | ||
- | } | ||
- | }); | ||
- | |||
- | |||
- | //收到联系人订阅请求的处理方法,具体的type值所对应的值请参考xmpp协议规范 | ||
- | var handlePresence = function ( e ) { | ||
- | |||
- | //对方收到请求加为好友 | ||
- | if (e.type === 'subscribe') { | ||
- | | ||
- | /*拒绝添加好友的方法处理*/ | ||
- | conn.unsubscribed({ | ||
- | to: 'username', | ||
- | message : 'rejectAddFriend' | ||
- | }); | ||
- | } | ||
- | } | ||
- | </sxh> | ||
- | |||
- | ===== 删除好友 ===== | ||
- | |||
- | <sxh javascript> | ||
- | // 删除好友 | ||
- | var removeFriends = function () { | ||
- | conn.removeRoster({ | ||
- | to: 'username', | ||
- | success: function () { // 删除成功 | ||
- | conn.unsubscribed({ | ||
- | to: 'username' | ||
- | }); | ||
- | }, | ||
- | error: function () { // 删除失败 | ||
- | } | ||
- | }); | ||
- | }; | ||
- | </sxh> | ||
- | |||
- | ===== 黑名单 ===== | ||
- | |||
- | ==== 将好友加入黑名单 ==== | ||
- | |||
- | 将好友加入黑名单后,对方好友列表依然可以看到已方,但无法向已方发送消息。 | ||
- | |||
- | <sxh javascript> | ||
- | // list的结构为{username_1: {}, username_2: {}...},拉黑好友需要将拉黑后的黑名单里的好友信息全部传入, | ||
- | // 如黑名单此时已有A,B两位好友,现想将C也拉进黑名单,正确的操作是同时将ABC的信息都传入接口中。 | ||
- | /* | ||
- | var list = { | ||
- | username_1:{ | ||
- | jid: 'appKey_'+username_1+'@easemob.com', | ||
- | name: username_1, | ||
- | subscription: 'both', | ||
- | order: 2 | ||
- | }, | ||
- | username_2:{ | ||
- | jid: 'appKey_'+username_2+'@easemob.com', | ||
- | name: username_2, | ||
- | subscription: 'both', | ||
- | order: 3, | ||
- | type: 'jid' | ||
- | }, | ||
- | username_3:{ | ||
- | jid: 'appKey_'+username_3+'@easemob.com', | ||
- | name: username_3, | ||
- | subscription: 'both', | ||
- | order: 4, | ||
- | type: 'jid' | ||
- | } | ||
- | } | ||
- | jid, username, subscription均在获取好友列表时已获取到,用户可根据好友列表动态获取这些参数, | ||
- | order不重复即可 | ||
- | */ | ||
- | var addToBlackList = function () { | ||
- | var list = { | ||
- | // user_1 | ||
- | asdfghj: { | ||
- | jid: 'easemob-demo#chatdemoui_asdfghj@easemob.com', | ||
- | name: 'asdfghj', | ||
- | subscription: 'both', | ||
- | order: 2, | ||
- | type: 'jid' | ||
- | }, | ||
- | // user_2 | ||
- | wjy6: { | ||
- | jid: 'easemob-demo#chatdemoui_wjy6@easemob.com', | ||
- | name: 'wjy6', | ||
- | subscription: 'both', | ||
- | order: 3, | ||
- | type: 'jid' | ||
- | } | ||
- | }; | ||
- | conn.addToBlackList({ | ||
- | list: list, | ||
- | type: 'jid', | ||
- | success: function () { | ||
- | console.log('Add friend to black list success'); | ||
- | }, | ||
- | error: function () { | ||
- | console.log('Add friend to black list error'); | ||
- | } | ||
- | }); | ||
- | } | ||
- | </sxh> | ||
- | |||
- | ==== 获取黑名单列表 ==== | ||
- | |||
- | 调用getBlacklist函数获取好友黑名单列表。调用这个函数时,会回调conn.listen里的onBlacklistUpdate函数,具体细节请参照[[im:400webimintegration:25intiate|Web SDK基础功能]]。 | ||
- | |||
- | <sxh javascript> | ||
- | var getBlackList = function () { | ||
- | conn.getBlacklist(); | ||
- | }; | ||
- | </sxh> | ||
- | |||
- | ==== 将好友移出黑名单 ==== | ||
- | |||
- | 将好友移出黑名单的机制是,将移出后某好友后黑名单中的好友组成一个list,传入接口重建黑名单。例如,黑名单中有ABC三个人,需要移出A,则将B和C组合成一个list传入接口。若list为空,则清空黑名单。 | ||
- | |||
- | <sxh javascript> | ||
- | // 将好友从黑名单拉出来 | ||
- | var removeBlackList = function () { | ||
- | var list = [ | ||
- | { | ||
- | wjy6: { | ||
- | jid: 'easemob-demo#chatdemoui_mengyuanyuan01@easemob.com', | ||
- | name: 'mengyuanyuan01', | ||
- | order: '2', | ||
- | type: 'jid' | ||
- | } | ||
- | }, | ||
- | { | ||
- | mengyuanyuan: { | ||
- | jid: 'easemob-demo#chatdemoui_wjy6@easemob.com', | ||
- | name: 'wjy6', | ||
- | order: '4', | ||
- | type: 'jid', | ||
- | } | ||
- | } | ||
- | ]; | ||
- | conn.removeFromBlackList({ | ||
- | list: list, | ||
- | type: 'jid', | ||
- | success: function () { | ||
- | console.log('Remove from black list success.'); | ||
- | }, | ||
- | error: function () { | ||
- | console.log('Remove from black list error.') | ||
- | } | ||
- | }); | ||
- | }; | ||
- | </sxh> | ||
- | |||
- | |||
- | ---- | ||
- | <WRAP group> | ||
- | <WRAP half column> | ||
- | 上一页:[[start:400webimintegration:30singlechat|消息]] | ||
- | </WRAP> | ||
- | |||
- | <WRAP half column> | ||
- | 下一页:[[start:400webimintegration:40groupchat|群聊]] | ||
- | </WRAP> | ||
- | </WRAP> |