基于环信SDK的IM即时通讯填坑之路(vue)
生活随笔
收集整理的這篇文章主要介紹了
基于环信SDK的IM即时通讯填坑之路(vue)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
公司最近使用第三方環(huán)信SDK的進(jìn)行通信聊天,基本已完成。記錄下填坑之路
1、可以通過以下方式引用 WebSDK
1.安裝
npm install easemob-websdk --save
2. 先 require ,再訪問 Web IM 。
require('easemob-websdk');
注:該方式只引用了 Web SDK ,仍需在項目里配置 WebIMConfig 文件內(nèi)的參數(shù),用于實例化 websdk。
配置在 webim.config.js 文件內(nèi)進(jìn)行以下配置:
xmppURL: 'im-api.easemob.com', // xmpp Server地址,對于在console.easemob.com創(chuàng)建的appKey,固定為該值 apiURL: 'http://a1.easemob.com', // rest Server地址,對于在console.easemob.com創(chuàng)建的appkey,固定為該值 appkey: 'easemob-demo#chatdemoui', // App key https : false, // 是否使用https isHttpDNS: true, //防止DNS劫持從服務(wù)端獲取XMPPUrl、restUrl isMultiLoginSessions: false, // 是否開啟多頁面同步收消息,注意,需要先聯(lián)系商務(wù)開通此功能 isAutoLogin: true, // 自動出席,(如設(shè)置為false,則表示離線,無法收消息,需要在登錄成功后手動調(diào)用conn.setPresence()才可以收消息) isDebug: false, // 打開調(diào)試,會自動打印log,在控制臺的console中查看log autoReconnectNumMax: 2, // 斷線重連最大次數(shù) autoReconnectInterval: 2, // 斷線重連時間間隔 heartBeatWait: 4500, // 使用webrtc(視頻聊天)時發(fā)送心跳包的時間間隔,單位ms delivery: true, // 是否發(fā)送已讀回執(zhí)
2、組件中使用(群聊)
這里以群聊為例子
注冊
regeister(id,name) {
var options = {
username: id,//用戶名
password: md5(id),//密碼
nickname: name,//昵稱
appKey: WebIM.config.appkey,//appkey
success: e => {
console.log('注冊成功', e);
if (e) {
localStorage.setItem("liveUser", id);//注冊成功將id保存
this.login()//去登錄
}
},
error: e => {
console.log("注冊異常",e);
if(e.type == 17){//如果已注冊 則登錄
console.log("注冊異常去登陸",e);
localStorage.setItem("liveUser", id);
this.login();
}
},
apiUrl: WebIM.config.apiURL
};
WebIM.conn.registerUser(options);
},
1、這里如果注冊異常的type==17則代表已注冊,那就直接去登錄
2、這里id是依據(jù)是否在應(yīng)用內(nèi)已登錄(自己應(yīng)用,非環(huán)信),
如果是未登錄(游客狀態(tài))則隨機(jī)一個
Math.ceil(Math.random() * 100000);
如果登錄,則用登錄后返回的user_id
登錄
//環(huán)信登錄
login() {
var userName = localStorage.getItem("liveUser");
var options = {
apiUrl: WebIM.config.apiURL,
user: userName,
pwd: md5(userName),
appKey: WebIM.config.appkey,
success: e => {
console.log('登錄成功', e);
if (e) {
this.addGroup()
}
},
fail: e => {
console.error('登錄失敗', e);
}
};
WebIM.conn.open(options);
},
登錄成功后就可以加入群組
加入群組
//加入群組
addGroup() {
var options = {
groupId: "xxxx", // 群組ID
success: function (resp) {
console.log("Response: ", resp);
},
error: function (e) {
console.error("加入群聊異常", e, JSON.parse(e.data));
if (e.type == 17) {
console.error("您已經(jīng)在這個群組里了");
}
}
};
WebIM.conn.joinGroup(options);
},
這里群id是你購買環(huán)信后創(chuàng)建應(yīng)用分配的群id
加群成功,就可以獲取群歷史消息了
獲取歷史消息
getMessList() {
var options = {
queue: "xxxx",//群組id
isGroup: true,//是否是群聊
count: 20,//返回多少條
success: data => {
this.$store.dispatch('receive_livemsglist',data)//這里把消息加到vuex中存儲
this.messList = this.$store.state.liveMsgList
console.log("獲取歷史消息", data)
},
fail: e => {
console.log("獲取群聊異常", e)
}
};
WebIM.conn.fetchHistoryMessages(options);
},
1、不能依據(jù)頁碼獲取數(shù)據(jù)
這里環(huán)信有個bug(也不能說bug,一個不足吧),就是獲取歷史消息竟然不是依據(jù)頁碼去拉取,也就是我拉取哪一頁就獲取哪一頁。沒辦法,環(huán)信沒有提供這個參數(shù)。
給環(huán)信提供了工單,他那邊反饋
解決
先說下,上面這個問題,就是用本地vuex存儲了.
vuex的state中
export default {
liveMsgList:[],//消息記錄
}
mutations.js中
//消息記錄
[RECEIVE_LIVEMSGLIST](state,{liveMsgList}){
if(state.liveMsgList.length){
state.liveMsgList.unshift(...liveMsgList)
}else{
state.liveMsgList = liveMsgList
}
},
actions.js中
receive_livemsglist({commit},liveMsgList){
commit(RECEIVE_LIVEMSGLIST,{liveMsgList})
},
2、獲取歷史消息時,會觸發(fā)監(jiān)聽接收消息事件
解決
這個是3.0.7 版本 SDK 的已知bug,建議您更新下SDK
監(jiān)聽消息
//環(huán)信接收消息
_IMListen() {
WebIM.conn.listen({
onOpened: () => { //連接成功回調(diào)
console.log("連接成功!")
},
//文本消息
onTextMessage: text => {
console.log("接收到了文本消息", text);
if (!text.error && text.type != "chat") {
this.messList.push(text);
}
},
//連接關(guān)閉回調(diào)
onClosed: function ( message ) {
console.log("連接關(guān)閉!",message)
},
//收到表情消息
onEmojiMessage: emj => {
console.log("接收到了表情消息", emj)
if (!emj.error && emj.type != "chat") {
this.messList.push(emj);
}
},
//cmd消息
onCmdMessage: ( message ) => {
console.log(message,"cmd")
},
//收到自定義消息
onCustomMessage: ( message ) => {
console.log(message,"Custom")
},
onError: e => {
console.log("接收消息錯誤", JSON.stringify(e));
},
onRecallMessage:(e)=>{
console.log("消息撤回",e)
},
});
},
發(fā)送消息
sendMess() {
var id = WebIM.conn.getUniqueId(); // 生成本地消息id
var msg = new WebIM.message("txt", id); // 創(chuàng)建文本消息
var option = {
msg: this.message, // 消息內(nèi)容
to: "xxx", // 接收消息對象(聊天室id)
roomType: false, // 群聊類型,true時為聊天室,false時為群組
ext: {}, // 擴(kuò)展消息
success: (id, serverMsgId) => {
console.log("發(fā)送消息成功",serverMsgId)
},
fail: e => {
console.error("發(fā)送消息異常", e)
}
};
msg.set(option);
msg.setGroup("groupchat"); // 群聊類型
WebIM.conn.send(msg.body);
}
},
大致流程就這樣,不懂的可以評論能解決就會回答。
總結(jié)
以上是生活随笔為你收集整理的基于环信SDK的IM即时通讯填坑之路(vue)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LVS负载均衡之NAT模式
- 下一篇: 2018第三届中国数字化零售创新国际峰会