云信IMweb端开发总结

477 阅读2分钟

公司移动端在ios 平台和 安卓平台已经实现了点对点的聊天功能,产品需求在pc端能实现消息同步和互联,因为是第一次使用,在五天之内开发一个基础版的功能,走了一些弯路在这里分享给大家,希望能够给大家一些帮助!

chat.gif
其中最重大的一个坑就是开发的思路的问题,因为我这里是点对点的聊天,其中没有群聊和好友的概念在里面,所以相对会简单一些。

  • 第一个步 我们需要初始化云信的IM函数
  var ni m = NIM.getInstance({
  // debug: true,
  appKey: 'appKey',
  account: 'account',
  token: 'token',
  // privateConf: {}, // 私有化部署方案所需的配置
  onconnect: onConnect,
  onwillreconnect: onWillReconnect,
  ondisconnect: onDisconnect,
  onerror: onError
});

appKey是你登录云信后台去取得,这里有个小坑就是acount 和token 都只能是小写,不能有大写,要不然取会话列表值的时候会辨识为不同会话列表!(切记)

  • 第二步
        onSessions: function(sessions) {
            this.sessions = nim.mergeSessions(this.sessions, sessions) // 这里使用sdk自带的方法合并会话列表重新赋值
            this.usDateSessionsUI(); //更新UI
        },
      //更新会话
        onUpdateSession: function (session) {
            this.sessions = nim.mergeSessions(this.sessions, session)
            this.usDateSessionsUI();//更新UI
        },

因为会话列表中并不包含头像等信息所以我们需要另一个接口给我提供这些数据

  • 第三步
var accounts = this.sessions.map(function(item) {
    if(item.lastMsg){
        if(item.lastMsg.flow == 'in'){
            return item.lastMsg.from;
        } else {
            return item.lastMsg.to;
        }
    }
})

我们要匹配出发进来的id进行匹配 得到我们的 acounts

 nim.getUsers({
  accounts: accounts,
  done: function(error, users){
      if (!error) {
          that.accountList = users.map(function(it, idx){
              for(var i = 0; i < that.sessions.length; i++) {
                  if(that.sessions[i].lastMsg.flow == 'in') {
                      if(it.account === that.sessions[i].lastMsg.from) {
                        // 自己定义
                      }
                  } else {
                      if(it.account === that.sessions[i].lastMsg.to) {
                       // 自己定义
                      }
                  }
              }
              return it;
          });
      }
  }
});

把acounts传入我们的 getUsers的接口返回我们的用户信息 在自定义中大家可以任意拼装我们的数据

  • 第四步
 // 点击每个会话列表中某个用户
targetUser: function(item, index) {
    var that = this;
    this.currentAcount = item.account;
    this.currentSessionId = item.sessionsId;
    // 设置当前会话 面板打开的时候才重新设置会话未读数量
    if(this.isBigPanel) nim.setCurrSession(item.sessionsId);
    this.currentFromInfo = item;
    this.currentIndex = item.account;
    this.getLocalMsg(); //获取本地聊天内容对话消息
    
},

我这里选用的获取本地的存储的接口数据,没有那么占用网络资源,速度也更优,但是如果换了机器会话记录就没有了,各区所需。

  • 第五步
  // 获取本地信息
getLocalMsg: function(){
    var that = this;
    nim.getLocalMsgs({
        sessionId: this.currentSessionId,
        limit: 1000,
        start: this.todayTime,
        end: Infinity,
        // desc: false,
        done: function (error, obj) {
            if (!error) {
                that.messageList = obj.msgs.reverse().map(function(item) {
                    // 如果是自定义消息 将 content 转 json
                    if(item.type === 'custom') {
                        item.content = JSON.parse(item.content);
                    }
                    return item;
                });
                that.scrollToBottom();
                // that.insertTimeDom();
            }
        }
    })
},

我这里默认取了1000条数据,如果有自定义的消息大家就根据判断取数据了。 如有问题不吝指教

-完-