消息分两种消息:1,收消息,发消息
1.收消息
收消息又分两种:1.即时消息和历史消息
即时消息就是在线的时候,对方发给你的消息。这主要是在上一篇里面的onMsgNotify方法里面获取。
历史消息就是你和某个用户聊过的最近7天的聊天记录。
我就主要写一下获得和指定用户的历史聊天记录,毕竟,即时消息获得的方法,上一篇有了嘛。
webim.getC2CHistoryMsgs(option,cbOk,cbErr);//主要是这个方法
var options = {
'Peer_Account': userId, //指定的好友帐号
'MaxCnt': reqMsgCount,//拉取的消息数目
'LastMsgTime': lastMsgTime,//上一次拉取的时间 在第一次拉去消息的时候,这里必须为0
'MsgKey': msgKey
}
var selSess = null;
webim.getC2CHistoryMsgs(
options,
function (resp) {
console.log(resp)
var complete = resp.Complete; //是否还有历史消息可以拉取,1-表示没有,0-表示有
if (resp.MsgList.length == 0) {
return
}
//拉取消息后,要将下一次拉取信息所需要的东西给存在缓存中
wx.setStorageSync('lastMsgTime', resp.LastMsgTime);
wx.setStorageSync('msgKey', resp.MsgKey);
var msgList = resp.MsgList;
for (var j in msgList) { //遍历新消息
var msg = msgList[j];
if (msg.getSession().id() == userId) { //为当前聊天对象的消息
selSess = msg.getSession();
//在聊天窗体中新增一条消息
that.addMsg(msg)
}
}
//消息已读上报,并将当前会话的消息设置成自动已读
webim.setAutoRead(selSess, true, true);
},
)
这里面其实最主要的方法是就是里面的addMsg。不管是收最新的消息,还是历史消息,都是需要解析后,才能再屏幕上展示的。
//处理消息(私聊(包括普通消息和全员推送消息),普通群(非直播聊天室)消息) 我这里是只要私聊的
addMsg:function(msg){
var that = this;
var fromAccount