|
@@ -32,59 +32,10 @@ export class MessageService {
|
|
public toastController: ToastController,
|
|
public toastController: ToastController,
|
|
private http: HttpService
|
|
private http: HttpService
|
|
) {}
|
|
) {}
|
|
- messageMapList: any = {
|
|
|
|
- global_room: [
|
|
|
|
- // 世界频道消息列表
|
|
|
|
- // {
|
|
|
|
- // is_self: true,
|
|
|
|
- // avatar:
|
|
|
|
- // 'https://file-cloud.fmode.cn/Qje9D4bqol/20241109/2t1lp0032258601.png',
|
|
|
|
- // msg_type: 1,
|
|
|
|
- // content: 'nihao',
|
|
|
|
- // istoday: true,
|
|
|
|
- // timestamp: new Date(),
|
|
|
|
- // },
|
|
|
|
- // {
|
|
|
|
- // is_self: true,
|
|
|
|
- // avatar:
|
|
|
|
- // 'https://file-cloud.fmode.cn/Qje9D4bqol/20241109/2t1lp0032258601.png',
|
|
|
|
- // msg_type: 1,
|
|
|
|
- // content: `Use the pipe name to trace where the pipe is declared and used. To resolve this error: If the pipe is local to the NgModule, give it a unique name in the pipe's decorator and declared it in the NgModule. If the pipe is standalone or is declared in another NgModule, add it to the imports field of the standalone component or the current NgModule.`,
|
|
|
|
- // istoday: true,
|
|
|
|
- // timestamp: new Date(),
|
|
|
|
- // },
|
|
|
|
- // {
|
|
|
|
- // is_self: true,
|
|
|
|
- // avatar:
|
|
|
|
- // 'https://file-cloud.fmode.cn/Qje9D4bqol/20241109/2t1lp0032258601.png',
|
|
|
|
- // msg_type: 1,
|
|
|
|
- // content: `Use the pipe name to trace where the pipe is declared and used. To resolve this error: If the pipe is local to the NgModule, give it a unique name in the pipe's decorator and declared it in the NgModule. If the pipe is standalone or is declared in another NgModule, add it to the imports field of the standalone component or the current NgModule.`,
|
|
|
|
- // istoday: true,
|
|
|
|
- // timestamp: new Date(),
|
|
|
|
- // },
|
|
|
|
- // {
|
|
|
|
- // is_self: true,
|
|
|
|
- // avatar:
|
|
|
|
- // 'https://file-cloud.fmode.cn/Qje9D4bqol/20241109/2t1lp0032258601.png',
|
|
|
|
- // msg_type: 1,
|
|
|
|
- // content: `Use the pipe name to trace where the pipe is declared and used. To resolve this error: If the pipe is local to the NgModule, give it a unique name in the pipe's decorator and declared it in the NgModule. If the pipe is standalone or is declared in another NgModule, add it to the imports field of the standalone component or the current NgModule.`,
|
|
|
|
- // istoday: true,
|
|
|
|
- // timestamp: new Date(),
|
|
|
|
- // },
|
|
|
|
- // {
|
|
|
|
- // is_self: false,
|
|
|
|
- // avatar:
|
|
|
|
- // 'https://file-cloud.fmode.cn/Qje9D4bqol/20241109/2t1lp0032258601.png',
|
|
|
|
- // msg_type: 1,
|
|
|
|
- // content: 'nihao',
|
|
|
|
- // istoday: true,
|
|
|
|
- // timestamp: new Date(),
|
|
|
|
- // },
|
|
|
|
- ],
|
|
|
|
- };
|
|
|
|
|
|
+ messageMapList: any = {};
|
|
|
|
|
|
/* 获取token */
|
|
/* 获取token */
|
|
- async getToken(channel?: string) {
|
|
|
|
|
|
+ async getToken() {
|
|
this.userId = Parse.User.current()?.id!;
|
|
this.userId = Parse.User.current()?.id!;
|
|
//获取频道token记录
|
|
//获取频道token记录
|
|
let uid = Parse.User.current()?.id;
|
|
let uid = Parse.User.current()?.id;
|
|
@@ -106,12 +57,12 @@ export class MessageService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- async initRTM(channelName: string) {
|
|
|
|
|
|
+ async initRTM() {
|
|
// let states = ['CONNECTED', 'CONNECTING'];
|
|
// let states = ['CONNECTED', 'CONNECTING'];
|
|
if (this.options.connectState) return;
|
|
if (this.options.connectState) return;
|
|
- await this.getToken(channelName);
|
|
|
|
- // const rtmConfig = { logLevel: 'debug' };
|
|
|
|
- this.rtmClient = new AgoraRTM.RTM(this.appid, this.userId);
|
|
|
|
|
|
+ await this.getToken();
|
|
|
|
+ const rtmConfig = { logLevel: 'debug' };
|
|
|
|
+ this.rtmClient = new AgoraRTM.RTM(this.appid, this.userId,rtmConfig);
|
|
this.joinReady();
|
|
this.joinReady();
|
|
await this.loginRTM();
|
|
await this.loginRTM();
|
|
// this.subscribeMessage(channelName);
|
|
// this.subscribeMessage(channelName);
|
|
@@ -123,7 +74,7 @@ export class MessageService {
|
|
*@'REFUSEINVITATION_' + uid: 拒绝通话邀请
|
|
*@'REFUSEINVITATION_' + uid: 拒绝通话邀请
|
|
*@'RESPONSEINVITOIN_' + uid: 接受邀请
|
|
*@'RESPONSEINVITOIN_' + uid: 接受邀请
|
|
*/
|
|
*/
|
|
- joinReady(channelName?: string) {
|
|
|
|
|
|
+ joinReady() {
|
|
this.rtmClient?.addEventListener('message', async (event: any) => {
|
|
this.rtmClient?.addEventListener('message', async (event: any) => {
|
|
console.log('接收到一条消息:', event);
|
|
console.log('接收到一条消息:', event);
|
|
let states = [
|
|
let states = [
|
|
@@ -135,8 +86,10 @@ export class MessageService {
|
|
const message = JSON.parse(event.message);
|
|
const message = JSON.parse(event.message);
|
|
let is_self = event.publisher == this.userId;
|
|
let is_self = event.publisher == this.userId;
|
|
console.log('自己发出的消息:', is_self, message.text);
|
|
console.log('自己发出的消息:', is_self, message.text);
|
|
- if (!is_self && states.includes(message.text)) {
|
|
|
|
- this.callPresence(message.text, event.publisher, event.channelName);
|
|
|
|
|
|
+ if (states.includes(message.text)) {
|
|
|
|
+ if (!is_self) {
|
|
|
|
+ this.callPresence(message.text, event.publisher, event.channelName);
|
|
|
|
+ }
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
this.showMessage(event);
|
|
this.showMessage(event);
|
|
@@ -165,7 +118,8 @@ export class MessageService {
|
|
Mode: mode,
|
|
Mode: mode,
|
|
};
|
|
};
|
|
try {
|
|
try {
|
|
- await this.rtmClient?.presence.setState(channelName, channelType, states);
|
|
|
|
|
|
+ let res = await this.rtmClient?.presence.setState(channelName, channelType, states);
|
|
|
|
+ console.log('频道状态发生更改:',res);
|
|
} catch (err: any) {
|
|
} catch (err: any) {
|
|
console.log(err);
|
|
console.log(err);
|
|
}
|
|
}
|
|
@@ -265,7 +219,7 @@ export class MessageService {
|
|
// console.error('join channel failed: ', status);
|
|
// console.error('join channel failed: ', status);
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
- async loginRTM(channelName?: string) {
|
|
|
|
|
|
+ async loginRTM() {
|
|
try {
|
|
try {
|
|
await this.rtmClient?.login({
|
|
await this.rtmClient?.login({
|
|
token: this.options.token,
|
|
token: this.options.token,
|
|
@@ -277,7 +231,7 @@ export class MessageService {
|
|
const metadata = [
|
|
const metadata = [
|
|
{
|
|
{
|
|
key: 'nickname',
|
|
key: 'nickname',
|
|
- value: user?.get('nickname'),
|
|
|
|
|
|
+ value: user?.get('nickname') || user?.get('name') || user?.id,
|
|
},
|
|
},
|
|
{
|
|
{
|
|
key: 'avatar',
|
|
key: 'avatar',
|
|
@@ -324,9 +278,9 @@ export class MessageService {
|
|
console.log('subscribeMessage', res);
|
|
console.log('subscribeMessage', res);
|
|
//订阅成功
|
|
//订阅成功
|
|
this.channelNameList[channelName] = true;
|
|
this.channelNameList[channelName] = true;
|
|
- if (this.messageMapList[channelName]?.length === 0) {
|
|
|
|
|
|
+ if (!this.messageMapList[channelName]) {
|
|
this.messageMapList[channelName] = [];
|
|
this.messageMapList[channelName] = [];
|
|
- this.getHistoryMessage(channelName)
|
|
|
|
|
|
+ this.getHistoryMessage(channelName);
|
|
}
|
|
}
|
|
this.pageFun?.();
|
|
this.pageFun?.();
|
|
resolve(true);
|
|
resolve(true);
|
|
@@ -344,15 +298,16 @@ export class MessageService {
|
|
query.descending('createdAt');
|
|
query.descending('createdAt');
|
|
query.skip(this.messageMapList[channelName].length);
|
|
query.skip(this.messageMapList[channelName].length);
|
|
query.limit(50);
|
|
query.limit(50);
|
|
- query.select('content','from_id');
|
|
|
|
|
|
+ query.select('content', 'from_id');
|
|
let msgList = await query.find();
|
|
let msgList = await query.find();
|
|
|
|
+ console.log('历史消息:', msgList);
|
|
msgList.forEach((item: any) => {
|
|
msgList.forEach((item: any) => {
|
|
let is_self = item?.get('from_id') == this.userId;
|
|
let is_self = item?.get('from_id') == this.userId;
|
|
let data: any = item?.get('content');
|
|
let data: any = item?.get('content');
|
|
data['is_self'] = is_self;
|
|
data['is_self'] = is_self;
|
|
data['istoday'] = true;
|
|
data['istoday'] = true;
|
|
- data['timestamp'] = new Date(data.timestamp)
|
|
|
|
- this.messageMapList[channelName].unshift(data)
|
|
|
|
|
|
+ data['timestamp'] = new Date(data.timestamp);
|
|
|
|
+ this.messageMapList[channelName].unshift(data);
|
|
});
|
|
});
|
|
// this.messageMapList[channelName].unshift(...msgList);
|
|
// this.messageMapList[channelName].unshift(...msgList);
|
|
}
|
|
}
|