环信IM (二)添加 删除

</pre>1.<pre name="code" class="html">//主动添加好友操作的实现方法
var startAddFriend = function() {
	var user = $('#addfridentId').val();
	if (user == '') {
		$('#add-frident-warning').html(
				"<font color='#FF0000'> 请输入好友名称</font>");
		return;
	}
	if (bothRoster)
		for (var i = 0; i < bothRoster.length; i++) {
			if (bothRoster[i].name == user) {
				$('#add-frident-warning').html(
						"<font color='#FF0000'> 已是您的好友</font>");
				return;
			}
		}
	//发送添加好友请求
	conn.subscribe({
		to : user,
		message : "加个好友呗-" + getLoacalTimeString()
	});
	$('#addFridentModal').modal('hide');
	return;
};
<pre name="code" class="html">bothRoster是在初始化时从服务器读取的。
 

2. 

//easemobwebim-sdk中处理出席状态操作

var handleRoster = function(rosterMsg) {
	for (var i = 0; i < rosterMsg.length; i++) {
		var contact = rosterMsg[i];
		if (contact.ask && contact.ask == 'subscribe') {
			continue;
		}
		if (contact.subscription == 'to') {
			toRoster.push({
				name : contact.name,
				jid : contact.jid,
				subscription : "to"
			});
		}
		//app端删除好友后web端要同时判断状态from做删除对方的操作
		if (contact.subscription == 'from') {
			toRoster.push({
				name : contact.name,
				jid : contact.jid,
				subscription : "from"
			});
		}
		if (contact.subscription == 'both') {
			var isexist = contains(bothRoster, contact);
			if (!isexist) {
				var lielem = $('<li>').attr({
					"id" : contact.name,
					"class" : "offline",
					"className" : "offline"
				}).click(function() {
					chooseContactDivClick(this);
				});
				$('<img>').attr({
					"src" : "static/img/head/contact_normal.png"
				}).appendTo(lielem);
				$('<span>').html(contact.name).appendTo(lielem);
				$('#contactlistUL').append(lielem);
				bothRoster.push(contact);
			}
		}
		if (contact.subscription == 'remove') {
			var isexist = contains(bothRoster, contact);
			if (isexist) {
				removeFriendDomElement(contact.name);
			}
		}
	}
};
3. 删除

//直接调用删除操作时的调用方法
var directDelFriend = function() {
	var user = $('#delfridentId').val();
	if (validateFriend(user, bothRoster)) {
		conn.removeRoster({
			to : user,
			success : function() {
				conn.unsubscribed({
					to : user
				});
				//删除操作成功时隐藏掉dialog
				$('#delFridentModal').modal('hide');
			},
			error : function() {
				$('#del-frident-warning').html(
						"<font color='#FF0000'>删除联系人失败!</font>");
			}
		});
	} else {
		$('#del-frident-warning').html(
				"<font color='#FF0000'>该用户不是你的好友!</font>");
	}
};
//判断要删除的好友是否在当前好友列表中
var validateFriend = function(optionuser, bothRoster) {
	for ( var deluser in bothRoster) {
		if (optionuser == bothRoster[deluser].name) {
			return true;
		}
	}
	return false;
};
4. 每次添加 删除好友 对面都会都到通知,但是无法收到对面接受 拒绝的通知
//easemobwebim-sdk中收到联系人订阅请求的处理方法,具体的type值所对应的值请参考xmpp协议规范
var handlePresence = function(e) {
	if (e.type == 'unavailable') {
		var el = null;

		if (e.chatroom && e.destroy) {
			el = document.getElementById(chatRoomMark + e.from)
		} else {
			el = document.getElementById(groupFlagMark + e.from)
		}
		el && $(el).remove();
		return;
	}
	//(发送者希望订阅接收者的出席信息),即别人申请加你为好友
	if (e.type == 'subscribe') {
		if (e.status) {
			if (e.status.indexOf('resp:true') > -1) {
				agreeAddFriend(e.from);
				return;
			}
		}
		var subscribeMessage = e.from + "请求加你为好友。\n验证消息:" + e.status;
		showNewNotice(subscribeMessage);
		$('#confirm-block-footer-confirmButton').click(function() {
			//同意好友请求
			agreeAddFriend(e.from);//e.from用户名
			//反向添加对方好友
			conn.subscribe({
				to : e.from,
				message : "[resp:true]"
			});
			$('#confirm-block-div-modal').modal('hide');
		});
		$('#confirm-block-footer-cancelButton').click(function() {
			rejectAddFriend(e.from);//拒绝加为好友
			$('#confirm-block-div-modal').modal('hide');
		});
		return;
	}
	//(发送者允许接收者接收他们的出席信息),即别人同意你加他为好友
	if (e.type == 'subscribed') {
		toRoster.push({
			name : e.from,
			jid : e.fromJid,
			subscription : "to"
		});
		return;
	}
	//(发送者取消订阅另一个实体的出席信息),即删除现有好友
	if (e.type == 'unsubscribe') {
		//单向删除自己的好友信息,具体使用时请结合具体业务进行处理
		delFriend(e.from);
		return;
	}
	//(订阅者的请求被拒绝或以前的订阅被取消),即对方单向的删除了好友
	if (e.type == 'unsubscribed') {
		delFriend(e.from);
		return;
	}
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值