在main.js中初始化GoEasy对象
import GoEasy from './lib/goeasy-2.4.7.min'; //在goeasy官网下载sdk到本地
const app = new Vue({
...App
});
const goEasy = GoEasy.getInstance({
host:'hangzhou.goeasy.io',//应用所在的区域地址: 【hangzhou.goeasy.io |singapore.goeasy.io】
appkey:'GoEasy appkey', // GoEasy增强型应用的appkey,官方建议使用profesional key + otp
modules:['im']
});
//将GoEasy和goEasy挂载到全局
Vue.prototype.GoEasy = GoEasy;
Vue.prototype.goEasy = goEasy;
登录成功后访问的第一个页面,建立GoEasy连接
if(this.goEasy.getConnectionStatus() === 'disconnected') {
this.goEasy.connect({
id: ‘当前登录用户的唯一标识id’,
data: {当前登录用户的信息,可以是json对象},
onSuccess: () => {
console.log('GoEasy connect successfully.')
},
onFailed: (error) => {
console.log('Failed to connect GoEasy, code:'+error.code+ ',error:'+error.content);
},
onProgress: (attempts) => {
console.log('GoEasy is connecting', attempts);
}
});
}
发送私信消息
let textMessage = this.goEasy.im.createTextMessage({
text: this.content,
to : {
id : '消息接收方用户的唯一标识id',
type : this.GoEasy.IM_SCENE.PRIVATE,
data : {消息接收方的用户信息,可以是json对象}
}
});
this.goEasy.im.sendMessage({
message: message,
onSuccess: function (message) {
console.log('发送成功.', message);
},
onFailed: function (error) {
if(error.code === 507){
console.log('发送语音/图片/视频/文件失败,没有配置OSS存储,详情参考:https://www.goeasy.io/cn/docs/goeasy-2.x/im/message/media/send-media-message.html');
}else{
console.log('发送失败:',error);
}
}
});
聊天页面实时接收私信消息
页面onload里监听私聊消息
this.goEasy.im.on(this.GoEasy.IM_EVENT.PRIVATE_MESSAGE_RECEIVED, (message) => {
// 这里收到的是所有私信给当前登录用户的消息,在渲染之前,需要判断是否是属于聊天对象的消息哦
console.log('PRIVATE_MESSAGE_RECEIVED:', message);
});
在页面onunload里清空私聊监听器中的内容
//退出聊天页面之前,清空监听器
this.goEasy.im.on(this.GoEasy.IM_EVENT.PRIVATE_MESSAGE_RECEIVED, ()=>{});
非聊天页面实时更新新的私信数量
监听会话列表更新事件获取实时未读总数量
this.goEasy.im.on(this.GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, (content) => {
console.log('当前用户总的未读消息数量:', content.unreadTotal); //拿到总未读数量之后就可以进行渲染新的新的私信数量了
});
0 个评论
要回复文章请先登录或注册