|
@@ -32,59 +32,10 @@ export class MessageService {
|
|
|
public toastController: ToastController,
|
|
|
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 */
|
|
|
- async getToken(channel?: string) {
|
|
|
+ async getToken() {
|
|
|
this.userId = Parse.User.current()?.id!;
|
|
|
//获取频道token记录
|
|
|
let uid = Parse.User.current()?.id;
|
|
@@ -106,12 +57,12 @@ export class MessageService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- async initRTM(channelName: string) {
|
|
|
+ async initRTM() {
|
|
|
// let states = ['CONNECTED', 'CONNECTING'];
|
|
|
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();
|
|
|
await this.loginRTM();
|
|
|
// this.subscribeMessage(channelName);
|
|
@@ -123,7 +74,7 @@ export class MessageService {
|
|
|
*@'REFUSEINVITATION_' + uid: 拒绝通话邀请
|
|
|
*@'RESPONSEINVITOIN_' + uid: 接受邀请
|
|
|
*/
|
|
|
- joinReady(channelName?: string) {
|
|
|
+ joinReady() {
|
|
|
this.rtmClient?.addEventListener('message', async (event: any) => {
|
|
|
console.log('接收到一条消息:', event);
|
|
|
let states = [
|
|
@@ -135,8 +86,10 @@ export class MessageService {
|
|
|
const message = JSON.parse(event.message);
|
|
|
let is_self = event.publisher == this.userId;
|
|
|
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;
|
|
|
}
|
|
|
this.showMessage(event);
|
|
@@ -165,7 +118,8 @@ export class MessageService {
|
|
|
Mode: mode,
|
|
|
};
|
|
|
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) {
|
|
|
console.log(err);
|
|
|
}
|
|
@@ -265,7 +219,7 @@ export class MessageService {
|
|
|
// console.error('join channel failed: ', status);
|
|
|
// }
|
|
|
// }
|
|
|
- async loginRTM(channelName?: string) {
|
|
|
+ async loginRTM() {
|
|
|
try {
|
|
|
await this.rtmClient?.login({
|
|
|
token: this.options.token,
|
|
@@ -277,7 +231,7 @@ export class MessageService {
|
|
|
const metadata = [
|
|
|
{
|
|
|
key: 'nickname',
|
|
|
- value: user?.get('nickname'),
|
|
|
+ value: user?.get('nickname') || user?.get('name') || user?.id,
|
|
|
},
|
|
|
{
|
|
|
key: 'avatar',
|
|
@@ -324,9 +278,9 @@ export class MessageService {
|
|
|
console.log('subscribeMessage', res);
|
|
|
//订阅成功
|
|
|
this.channelNameList[channelName] = true;
|
|
|
- if (this.messageMapList[channelName]?.length === 0) {
|
|
|
+ if (!this.messageMapList[channelName]) {
|
|
|
this.messageMapList[channelName] = [];
|
|
|
- this.getHistoryMessage(channelName)
|
|
|
+ this.getHistoryMessage(channelName);
|
|
|
}
|
|
|
this.pageFun?.();
|
|
|
resolve(true);
|
|
@@ -344,15 +298,16 @@ export class MessageService {
|
|
|
query.descending('createdAt');
|
|
|
query.skip(this.messageMapList[channelName].length);
|
|
|
query.limit(50);
|
|
|
- query.select('content','from_id');
|
|
|
+ query.select('content', 'from_id');
|
|
|
let msgList = await query.find();
|
|
|
+ console.log('历史消息:', msgList);
|
|
|
msgList.forEach((item: any) => {
|
|
|
let is_self = item?.get('from_id') == this.userId;
|
|
|
let data: any = item?.get('content');
|
|
|
data['is_self'] = is_self;
|
|
|
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);
|
|
|
}
|