点击链接先查看一下apicloud 环信的文档

https://docs.apicloud.com/Client-API/Open-SDK/easeChat

文档中写了很多,但官方给的文档还是有问题,之前做环信就被坑了一把,没有实现自己想要的功能,最后把所有的数据都推翻了以后,重新又写了一番,最终只采用了官方文档中的登录,退出登录,发送消息,与接收消息四个地方,聊天界面学员自己搭建,同时聊天的会话记录,页需要自己搭建,聊天消息的存储,等等

下面我只把精华的部分,登录与退出登录可参考官方文档,重点说一下搭建聊天,晒出,有兴趣的伙伴可以按照步骤去了解,

1.首先需要在apicloud中引模板easeChat,之后再html页面调用

var easeChat = api.require('easeChat');

2.之后我们需要确定的就是,单人聊天,还是多人聊天,在这里我只讲单人聊天

3.单人聊天,如果你有10个好友,那你需要创建11个本地文件,10个好友聊天记录文件,1个会话列表文件

4.本地存储文件看下面

                            api.writeFile({
path: 'cache://' + this_obj.info.your + '.txt', //this_obj.info.your存储的是对方的环信账号
data: JSON.stringify(ret.message) + ';', //ret.message
append: true
}, function(res, err) {
if (res.status) { } else {
api.alert({
msg: JSON.stringify(err)
});
}
});

5.读取存储文件看下面

                      api.readFile({
path: 'cache://' + this_obj.info.your + '.txt' //this_obj.info.your读取存储对方的环信账号
}, function(ret, err) {
if (ret.status) {
var array = [];
array = ret.data.substring(, ret.data.length - ).split(';')
this_obj.imgChat = []
for (i in array) {
var objectChat = JSON.parse(array[i])
this_obj.chatMsg.push(objectChat) //将会话截成数组,组成聊天记录
}
} else {
api.toast({
msg: '没有聊天记录'
});
}
});

6.聊天记录搭建好了,开始搭建会话列表了,会话列表需要好友头像,本人头像,好友环信名字,本人环信名字,未读消息,最后一条消息的时间,还有最后一条消息,共7点,前5点需要从好友列表的中得到,未读消息需要默认每个好友未读条数都为0,将一个人的数据组成对象,然后好友有多个,需要把这些都放在数组中,

后2点,需要你接收消息的时候将返回的数据,根据环信的名字去判断是你的哪个好友,然后把信息放在会话列表中,这里说的太多了,需要给你们看一下文件

 (1) 这个是好友列表本地存储文件common_ajax这个是调用的ajax封装的方法,

                        var data = {token: this_obj.token,us_id: ''};
var url = "/Info/my_friend";
common_ajax(url,data,function(res){
this_obj.list = res.info
for (i in this_obj.list.list) {
if (typeof this_obj.newChat_int.your_info === 'undefined') {
this_obj.newChat_int.your_info = {}
this_obj.newChat_int.your_info = this_obj.list.list[i].info.us_nickname
} else {
this_obj.newChat_int.your_info = this_obj.list.list[i].info.us_nickname
}
if (typeof this_obj.newChat_int.your_img === 'undefined') {
this_obj.newChat_int.your_img = {}
this_obj.newChat_int.your_img = this_obj.list.list[i].info.us_img
} else {
this_obj.newChat_int.your_img = this_obj.list.list[i].info.us_img
}
if (typeof this_obj.newChat_int.yourId === 'undefined') {
this_obj.newChat_int.yourId = {}
this_obj.newChat_int.yourId = this_obj.list.list[i].info.us_hx_username
} else {
this_obj.newChat_int.yourId = this_obj.list.list[i].info.us_hx_username
}
if (typeof this_obj.newChat_int.my_Name === 'undefined') {
this_obj.newChat_int.my_Name = {}
this_obj.newChat_int.my_Name = this_obj.list.list[i].my.us_nickname
} else {
this_obj.newChat_int.my_Name = this_obj.list.list[i].my.us_nickname
}
if (typeof this_obj.newChat_int.my_img === 'undefined') {
this_obj.newChat_int.my_img = {}
this_obj.newChat_int.my_img = this_obj.list.list[i].my.us_img
} else {
this_obj.newChat_int.my_img = this_obj.list.list[i].my.us_img
}
if (typeof this_obj.newChat_int.unreadNum === 'undefined') {
this_obj.newChat_int.unreadNum = {}
this_obj.newChat_int.unreadNum =
} else {
this_obj.newChat_int.unreadNum =
}
                                if (typeof this_obj.newChat_int.chat=== 'undefined') {
this_obj.newChat_int.chat= {}
} else {
this_obj.newChat_int.chat= {}
}

                                api.writeFile({
path: 'cache://chat_img_txt.txt',
data: JSON.stringify(this_obj.newChat_int) + ';',
append: true
}, function(res, err) {
if (res.status) {
// alert('好友存储')
} else {
api.alert({
msg: JSON.stringify(err)
});
}
});
}
},function(res){
api.toast({
msg: res.info
});
});

7.接收到消息以后,会得到的消息的messageId,这个id其实就是好友的环信名字,将会话列表循环用每个好友的环信名字与这个消息的id去对比,如果相同,那么就把这个接收到的消息,放在会话列表下这个好友的chat对象中,就可以了,

接收的时候可能在聊天页面,也可能在聊天外的页面,所以需要全局监听这个聊天信息,你可以在app.js中写一个全局的方法,这样方便去使用,还有提醒的是,接收到的消息有可能是一个数组,所以,需要循环去拿到messageId,下面看一下代码

        easeChat.addMessageListener({
name: 'receive'
}, function(ret) {
if (ret.messages) {
var firstListTxt = {};
firstListTxt = app.firstListTxt
for (var i = ; i < ret.messages.length; i++) {
if (ret.messages[i].from == app.info.your) {
api.writeFile({
path: 'cache://' + app.info.your + '.txt',
data: JSON.stringify(ret.messages[i]) + ';',
append: true
}, function(res, err) {
if (res.status) {
// alert('接收已存储')
} else { }
}); var objectChat = ret.messages[i]
var time = objectChat.timestamp.toString()
objectChat.timestamp = formatDate(time.substring(, time.length - )) if (typeof objectChat.body.arr === 'undefined') {
objectChat.body.arr = [];
objectChat.body.arr = app.emotionFun(objectChat.body.text)
} else {
objectChat.body.arr = app.emotionFun(objectChat.body.text)
}
} else {
var messages = ret.messages[i]
var arrayt = [];
arrayt = firstListTxt.data.substring(, firstListTxt.data.length - ).split(';') for (j in arrayt) {
var objectChat = JSON.parse(arrayt[j])
if (objectChat.yourId == messages.from) {
var time = messages.timestamp.toString()
messages.timestamp = formatDate(time.substring(, time.length - ))
objectChat.unreadNum = objectChat.unreadNum +
app.chat_int = objectChat
app.newChat_int = objectChat
//存储fistlist
api.writeFile({
path: 'cache://' + messages.from + '.txt',
data: JSON.stringify(messages) + ';',
append: true
}, function(res, err) {
if (res.status) {
// alert('存储对方会话成功')
app.chatSever(messages)
} else {
api.alert({
msg: JSON.stringify(err)
});
}
});
}
}
}
}
}
});

apicloud 环信总结的更多相关文章

  1. iOS-即时通讯-环信

    下载地址:http://www.easemob.com/downloads SDK目录讲解 1.从官网下载下来的包分为如下四部分: 环信iOS SDK 开发使用 环信iOS release note ...

  2. 环信Restfull API dotnetSDK

    Easemob.Restfull4Net 环信Restfull API dotnet的封装 支持的.Net Framework版本:4.0 API地址:http://docs.easemob.com/ ...

  3. 环信SDK集成

    利用环信SDK可以实现即时通讯,但在集成的过程中碰到了不少的坑. 注意 选择项目路径,这里以最新版环信demo为例 注意:环信的ChatDemoUI这个demo里边因为研发的同事为了照顾老版本的And ...

  4. 集成IOS 环信SDK

    集成IOS SDK 在您阅读此文档时,我们假定您已经具备了基础的 iOS 应用开发经验,并能够理解相关基础概念. 下载SDK 通过Cocoapods下载地址 不包含实时语音版本SDK(EaseMobC ...

  5. 环信SDK与Apple Watch的结合(2)

    这一篇主要是介绍怎么拖apple watch上的相关页面,附源码EMWatchOCDemo. 需要在工程中的“EMWatchOCDemo WatchKit App”中进行操作,该文件夹的结构如图 Wa ...

  6. 环信SDK与Apple Watch的结合(1)

    该系列是记录在apple watch上开发IM,用到了最近挺流行的环信IM SDK. 一.先来一段网上随处可查到的信息: 1.两种分辨率 1.65寸 312*390 1.5寸 272*340 2.开发 ...

  7. Android 环信的使用

    1.导入包 http://docs.easemob.com/doku.php?id=start:200androidcleintintegration:10androidsdkimport 在清单文件 ...

  8. 环信ipv6适配

    环信2.2.5及之后版本才适配了ipv6.可以自己搭配个ipv6环境,在ipv6环境下2.2.5以下版本无法登录.把整个sdk换成2.2.5版本项目需要改动的地方实在太多. 那么就部分换一下,适配ip ...

  9. 李洪强iOS开发本人集成环信的经验总结_01环信SDK的导入

    李洪强iOS开发本人集成环信的经验总结_01环信SDK的导入 01 - 直接在项目中导入SDK和一些静态库 这个时候,没有错误的编译没有错误的话,就说明SDK已经配置成功 还有一种方法是用cocoap ...

随机推荐

  1. 何为TLC、MLC、SLC?【转】

    本文转载自:https://blog.csdn.net/weixin_38233274/article/details/79310316 1.一块SSD由主控.DRAM缓存和NAND闪存三种芯片所组成 ...

  2. noip模拟【tea】

    tea [题目描述]有n个容量为V的瓶子,第i个瓶子中装着a[i]个单位的tea,使所有瓶子内的tea在不 超过其容量的前提下,非空的瓶子最少.在一个单位时间内,可以同时将多个瓶子中的tea倒入另外多 ...

  3. POJ 1251 + HDU 1301 Jungle Roads 【最小生成树】

    题解 这是一道裸的最小生成树题,拿来练手,题目就不放了 个人理解  Prim有些类似最短路和贪心,不断找距当前点最小距离的点 Kruskal类似于并查集,不断找最小的边,如果不是一棵树的节点就合并为一 ...

  4. cat查看文件以及sed查看指定行数

    https://unix.stackexchange.com/questions/288521/with-the-linux-cat-command-how-do-i-show-only-certai ...

  5. 【问题解决:死锁】Lock wait timeout exceeded; try restarting transaction的问题

    执行数据删除操作时一直超时并弹出Lock wait timeout exceeded; try restarting transaction错误 解决办法 1.先查看数据库的事务隔离级别 select ...

  6. LuoguP5221 Product

    题目地址 题目链接 题解 注,下方\((i,j)\)均指\(gcd(i,j)\),以及证明过程有一定的跳步,请确保自己会莫比乌斯反演的基本套路. 介绍本题的\(O(n)\)和\(O(n\sqrt{n} ...

  7. Vim的一些使用

    Vim的三种模式 normal(普通模式) insert(插入模式) command(命令模式) Vim的工作方式不同于常规的编辑器,在常规编辑器下对应到Vim中就是一直使用insert模式进行操作, ...

  8. [ECharts] - ECharts使用中国地图

    格式1: https://www.cnblogs.com/luna666/p/9007263.html  (非官方) <!DOCTYPE html> <html lang=" ...

  9. k8s2

    1.主节点与子节点如何沟通,交互 apiServer <==> kublet 2. pod之间如何共享, 使用volumn(数据卷 ) kube-proxy 和 service 配置好网络 ...

  10. Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project ppcloud-common: Compilation failure

    本文为博主原创,未经允许不得转载: [INFO] Finished at: 2018-09-19T20:26:05+08:00[INFO] ------------------------------ ...