|
@@ -13,14 +13,16 @@ export {AgoraRTMessage,AgoraRTMIMStatus,AgoraRTPcMessage}
|
|
|
|
|
|
export default class AgoraRTMIM extends Notice {
|
|
export default class AgoraRTMIM extends Notice {
|
|
|
|
|
|
- socket:any = undefined
|
|
|
|
|
|
+ static socket:any = undefined
|
|
|
|
+
|
|
|
|
+ static socketStatus:AgoraRTMIMStatus
|
|
|
|
|
|
socketChannel:any = undefined
|
|
socketChannel:any = undefined
|
|
|
|
|
|
constructor() {
|
|
constructor() {
|
|
super();
|
|
super();
|
|
- if (this.socket === undefined) {
|
|
|
|
- this.socket = AgoraRTM.createInstance(config.appKey);
|
|
|
|
|
|
+ if (AgoraRTMIM.socket === undefined) {
|
|
|
|
+ AgoraRTMIM.socket = AgoraRTM.createInstance(config.appKey);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -28,28 +30,52 @@ export default class AgoraRTMIM extends Notice {
|
|
return new Promise((resolve, reject)=>{
|
|
return new Promise((resolve, reject)=>{
|
|
this.setStatus(AgoraRTMIMStatus.loginIng);
|
|
this.setStatus(AgoraRTMIMStatus.loginIng);
|
|
|
|
|
|
- return this.socket.login({
|
|
|
|
- // token: user.user && user.user.login_token || '',
|
|
|
|
- uid: user.uid().toString()
|
|
|
|
- }).then((data)=>{
|
|
|
|
|
|
+ if (AgoraRTMIM.socketStatus === AgoraRTMIMStatus.login) {
|
|
|
|
+ this.joinSocket(roomId,resolve, reject);
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ AgoraRTMIM.socket.on(AgoraRTMessage.userMessage,(data)=>{
|
|
|
|
|
|
- this.setStatus(AgoraRTMIMStatus.joinIng);
|
|
|
|
- this.socketChannel = this.socket.createChannel(roomId);
|
|
|
|
- this.installSocketMessage();
|
|
|
|
- return this.socketChannel.join().then(()=>{
|
|
|
|
- this.setStatus(AgoraRTMIMStatus.join);
|
|
|
|
|
|
+ let resultData;
|
|
|
|
+ // 解析数据
|
|
|
|
+ try {
|
|
|
|
+ resultData = JSON.parse(data.text);
|
|
|
|
+ } catch (e) {
|
|
|
|
+ resultData = {
|
|
|
|
+ messageType: data.text
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.triggerMessage(resultData,data,AgoraRTMessage.userMessage);
|
|
|
|
+ });
|
|
|
|
|
|
- return resolve(data);
|
|
|
|
|
|
+ return AgoraRTMIM.socket.login({
|
|
|
|
+ // token: user.user && user.user.login_token || '',
|
|
|
|
+ uid: user.uid().toString()
|
|
|
|
+ }).then((data)=>{
|
|
|
|
+ AgoraRTMIM.socketStatus = AgoraRTMIMStatus.login;
|
|
|
|
+ this.joinSocket(roomId,resolve, reject);
|
|
}).catch(()=>{
|
|
}).catch(()=>{
|
|
- this.setStatus(AgoraRTMIMStatus.joinFail);
|
|
|
|
|
|
+ this.setStatus(AgoraRTMIMStatus.loginFail);
|
|
reject();
|
|
reject();
|
|
});
|
|
});
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
|
|
- }).catch(()=>{
|
|
|
|
- this.setStatus(AgoraRTMIMStatus.loginFail);
|
|
|
|
- reject();
|
|
|
|
- });
|
|
|
|
|
|
+ // join
|
|
|
|
+ joinSocket(roomId,resolve, reject){
|
|
|
|
+ this.setStatus(AgoraRTMIMStatus.joinIng);
|
|
|
|
+ this.socketChannel = AgoraRTMIM.socket.createChannel(roomId);
|
|
|
|
+ this.installSocketMessage();
|
|
|
|
+ this.socketChannel.join().then(()=>{
|
|
|
|
+ this.setStatus(AgoraRTMIMStatus.join);
|
|
|
|
+
|
|
|
|
+ return resolve();
|
|
|
|
+ }).catch(()=>{
|
|
|
|
+ this.setStatus(AgoraRTMIMStatus.joinFail);
|
|
|
|
+ reject();
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
@@ -72,19 +98,20 @@ export default class AgoraRTMIM extends Notice {
|
|
resultData = {};
|
|
resultData = {};
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
this.triggerMessage(resultData,data);
|
|
this.triggerMessage(resultData,data);
|
|
});
|
|
});
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- triggerMessage(resultData,data={}){
|
|
|
|
|
|
+ triggerMessage(resultData,data={},type=AgoraRTMessage.message){
|
|
|
|
|
|
if (this.watch[resultData.messageType]) {
|
|
if (this.watch[resultData.messageType]) {
|
|
resultData = this.watch[resultData.messageType](resultData);
|
|
resultData = this.watch[resultData.messageType](resultData);
|
|
}
|
|
}
|
|
|
|
|
|
// 触发 消息
|
|
// 触发 消息
|
|
- this.triggerListener(AgoraRTMessage.message, {
|
|
|
|
|
|
+ this.triggerListener(type, {
|
|
...(data || {}),
|
|
...(data || {}),
|
|
data:resultData,
|
|
data:resultData,
|
|
type: resultData.messageType
|
|
type: resultData.messageType
|
|
@@ -98,16 +125,32 @@ export default class AgoraRTMIM extends Notice {
|
|
}
|
|
}
|
|
|
|
|
|
watch = {
|
|
watch = {
|
|
- [AgoraRTMessage.chat]:function(item){
|
|
|
|
-
|
|
|
|
- // 如果存在进场转换消息类型
|
|
|
|
- if (item.star_img || item.star_play_img || item.star_entry_room) {
|
|
|
|
- item.messageType = AgoraRTMessage.joinHome;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return item;
|
|
|
|
|
|
+ // [AgoraRTMessage.chat]:function(item){
|
|
|
|
+ // // 如果存在进场转换消息类型
|
|
|
|
+ // if ((item.star_img || item.star_play_img || item.star_entry_room) && item.message.indexOf('进入直播间') >= 0 ) {
|
|
|
|
+ // item.messageType = AgoraRTMessage.joinHome;
|
|
|
|
+ // }
|
|
|
|
+ //
|
|
|
|
+ // return item;
|
|
|
|
+ //
|
|
|
|
+ // }
|
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ // 发送点对点消息
|
|
|
|
+ sendUser(data,type=AgoraRTMessage.chat,uid:number | string){
|
|
|
|
+ return new Promise((resolve, reject)=>{
|
|
|
|
+ if (this.status === AgoraRTMIMStatus.join) {
|
|
|
|
+ data.messageType = type;
|
|
|
|
+ if (this.sendWatch[type]) {
|
|
|
|
+ data = this.sendWatch[type](data);
|
|
|
|
+ }
|
|
|
|
+ AgoraRTMIM.socket && AgoraRTMIM.socket.sendMessageToPeer({
|
|
|
|
+ text:JSON.stringify(data)
|
|
|
|
+ },uid.toString()).then(resolve).catch(reject);
|
|
|
|
+ } else {
|
|
|
|
+ reject();
|
|
|
|
+ }
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
// 发送消息
|
|
// 发送消息
|
|
@@ -128,10 +171,10 @@ export default class AgoraRTMIM extends Notice {
|
|
}
|
|
}
|
|
|
|
|
|
sendWatch = {
|
|
sendWatch = {
|
|
- [AgoraRTMessage.joinHome]:function(item){
|
|
|
|
- item.messageType = AgoraRTMessage.chat;
|
|
|
|
- return item;
|
|
|
|
- }
|
|
|
|
|
|
+ // [AgoraRTMessage.joinHome]:function(item){
|
|
|
|
+ // item.messageType = AgoraRTMessage.chat;
|
|
|
|
+ // return item;
|
|
|
|
+ // }
|
|
}
|
|
}
|
|
|
|
|
|
// 追加用户信息
|
|
// 追加用户信息
|
|
@@ -165,6 +208,21 @@ export default class AgoraRTMIM extends Notice {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 追加全服消息
|
|
|
|
+ addGlobal(data,roomInfo){
|
|
|
|
+ if (data) {
|
|
|
|
+
|
|
|
|
+ data.roomInfo = {
|
|
|
|
+ id: roomInfo.rid,
|
|
|
|
+ name: roomInfo.room_name
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return data;
|
|
|
|
+ } else {
|
|
|
|
+ return data;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
// 追加礼物信息
|
|
// 追加礼物信息
|
|
addGiftInfo(data,toGiftData){
|
|
addGiftInfo(data,toGiftData){
|
|
if (data) {
|
|
if (data) {
|
|
@@ -219,8 +277,8 @@ export default class AgoraRTMIM extends Notice {
|
|
destroy() {
|
|
destroy() {
|
|
super.destroy();
|
|
super.destroy();
|
|
this.socketChannel && this.socketChannel.leave();
|
|
this.socketChannel && this.socketChannel.leave();
|
|
- this.socket && this.socket.logout();
|
|
|
|
- this.socket = undefined;
|
|
|
|
|
|
+ AgoraRTMIM.socket && AgoraRTMIM.socket.logout();
|
|
|
|
+ AgoraRTMIM.socket = undefined;
|
|
this.socketChannel = undefined;
|
|
this.socketChannel = undefined;
|
|
}
|
|
}
|
|
|
|
|