|
@@ -9,169 +9,274 @@ declare const AgoraRTM: any;
|
|
|
})
|
|
|
export class MessageService {
|
|
|
rtmClient: any;
|
|
|
+
|
|
|
+ channelNameList: any = {};
|
|
|
msChannelName: string = 'global_room';
|
|
|
options: any = {
|
|
|
- appid: '',
|
|
|
- userId: Parse.User.current()?.id,
|
|
|
- token:
|
|
|
- '007eJxTYGh7sUrIjKd9bl9ncuOq0mvLordY/r5dWv64Y3Wz5uVrNcwKDMmJBqbmJpaGickWliapFkYWBkYWqclp5sbmaSaphhYm+juD0hsCGRlOuFsyMDIwATEjA4jPxZBjkO5maeoU4OMEACaMIUE=',
|
|
|
+ connectState: false,
|
|
|
};
|
|
|
+ appid?: string;
|
|
|
+ userId: string = Parse.User.current()?.id!;
|
|
|
+
|
|
|
+ pageFun?: Function;
|
|
|
constructor(private liveService: LiveService, private http: HttpService) {
|
|
|
- this.initRTM();
|
|
|
+
|
|
|
}
|
|
|
- messageList: any = [
|
|
|
- {
|
|
|
- is_self: true,
|
|
|
- avatar:
|
|
|
- 'https://file-cloud.fmode.cn/Qje9D4bqol/20241109/2t1lp0032258601.png',
|
|
|
- msg_type: 1,
|
|
|
- content: 'nihao',
|
|
|
- create_time: new Date(),
|
|
|
- istoday: true,
|
|
|
- timeStamp: new Date(),
|
|
|
- data: [
|
|
|
- {
|
|
|
- create_time: 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.`,
|
|
|
- create_time: new Date(),
|
|
|
- istoday: true,
|
|
|
- timeStamp: new Date(),
|
|
|
- data: [
|
|
|
- {
|
|
|
- create_time: new Date(),
|
|
|
- },
|
|
|
- ],
|
|
|
- },
|
|
|
- {
|
|
|
- is_self: false,
|
|
|
- avatar:
|
|
|
- 'https://file-cloud.fmode.cn/Qje9D4bqol/20241109/2t1lp0032258601.png',
|
|
|
- msg_type: 1,
|
|
|
- content: 'nihao',
|
|
|
- create_time: new Date(),
|
|
|
- istoday: true,
|
|
|
- timeStamp: new Date(),
|
|
|
- data: [
|
|
|
- {
|
|
|
- create_time: new Date(),
|
|
|
- },
|
|
|
- ],
|
|
|
- },
|
|
|
- ];
|
|
|
+ messageMapList: any = {
|
|
|
+ global_room: [
|
|
|
+
|
|
|
+ {
|
|
|
+ is_self: true,
|
|
|
+ avatar:
|
|
|
+ 'https://file-cloud.fmode.cn/Qje9D4bqol/20241109/2t1lp0032258601.png',
|
|
|
+ msg_type: 1,
|
|
|
+ content: 'nihao',
|
|
|
+ create_time: new Date(),
|
|
|
+ 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.`,
|
|
|
+ create_time: new Date(),
|
|
|
+ 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.`,
|
|
|
+ create_time: new Date(),
|
|
|
+ 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.`,
|
|
|
+ create_time: new Date(),
|
|
|
+ istoday: true,
|
|
|
+ timeStamp: new Date(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ is_self: false,
|
|
|
+ avatar:
|
|
|
+ 'https://file-cloud.fmode.cn/Qje9D4bqol/20241109/2t1lp0032258601.png',
|
|
|
+ msg_type: 1,
|
|
|
+ content: 'nihao',
|
|
|
+ create_time: new Date(),
|
|
|
+ istoday: true,
|
|
|
+ timeStamp: new Date(),
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ };
|
|
|
+
|
|
|
|
|
|
- async getToken(channel: string) {
|
|
|
+ async getToken(channel?: string) {
|
|
|
|
|
|
let uid = Parse.User.current()?.id;
|
|
|
let baseurl = 'https://server.fmode.cn/api/webrtc/build_token';
|
|
|
let reqBody = {
|
|
|
company: this.liveService.company,
|
|
|
- channelName: channel,
|
|
|
+ channelName: this.msChannelName,
|
|
|
type: 'withrtm',
|
|
|
account: uid,
|
|
|
};
|
|
|
let data: any = await this.http.httpRequst(baseurl, reqBody, 'POST');
|
|
|
console.log(data);
|
|
|
if (data.code == 200) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
this.options.token = data.data.token;
|
|
|
- this.options.appid = data.data.appid;
|
|
|
+ this.appid = data.data.appid;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- async initRTM() {
|
|
|
- await this.getToken(this.msChannelName);
|
|
|
- console.log();
|
|
|
- this.rtmClient = new AgoraRTM.RTM(this.options.appid, this.options.userId);
|
|
|
+ async initRTM(channelName: string) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (this.options.connectState) return;
|
|
|
+ await this.getToken(channelName);
|
|
|
+ this.rtmClient = new AgoraRTM.RTM(this.appid, this.userId);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
this.joinReady();
|
|
|
await this.loginRTM();
|
|
|
-
|
|
|
- this.subscribeMessage(this.msChannelName);
|
|
|
+ this.subscribeMessage(channelName);
|
|
|
}
|
|
|
|
|
|
- joinReady() {
|
|
|
+ joinReady(channelName?: string) {
|
|
|
this.rtmClient.addEventListener('message', (event: any) => {
|
|
|
- this.showMessage(event.publisher, event.message);
|
|
|
+ console.log('message', event);
|
|
|
+ this.showMessage(event);
|
|
|
});
|
|
|
- console.log('initRTM');
|
|
|
-
|
|
|
this.rtmClient.addEventListener('presence', (event: any) => {
|
|
|
- console.log('presence event: ', event);
|
|
|
- if (event.eventType === 'SNAPSHOT') {
|
|
|
- this.showMessage('INFO', 'I Join');
|
|
|
- } else {
|
|
|
- this.showMessage('INFO', event.publisher + ' is ' + event.eventType);
|
|
|
- }
|
|
|
+ console.log('频道人员状态变化 ', event);
|
|
|
+ this.pageFun?.();
|
|
|
});
|
|
|
- this.rtmClient.addEventListener('status', (event: any) => {
|
|
|
- console.log('status event: ', event);
|
|
|
-
|
|
|
- const currentState = event.state;
|
|
|
- const changeReason = event.reason;
|
|
|
-
|
|
|
+ this.rtmClient.addEventListener('linkState', (event: any) => {
|
|
|
+ console.log('连接状态: ', event);
|
|
|
+
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- async join() {
|
|
|
- const options = {
|
|
|
- token: this.options.token,
|
|
|
- withPresence: true,
|
|
|
- withLock: true,
|
|
|
- withMetadata: true,
|
|
|
- };
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ async loginRTM(channelName?: string) {
|
|
|
try {
|
|
|
- const streamChannel = this.rtmClient.createStreamChannel(
|
|
|
- this.msChannelName
|
|
|
- );
|
|
|
- const result = await streamChannel.join(options);
|
|
|
- } catch (status) {
|
|
|
- console.error('join channel failed: ', status);
|
|
|
- }
|
|
|
- }
|
|
|
- async loginRTM() {
|
|
|
-
|
|
|
- try {
|
|
|
- const result = await this.rtmClient.login({ token: this.options.token });
|
|
|
-
|
|
|
+ await this.rtmClient.login({
|
|
|
+ token: this.options.token,
|
|
|
+ });
|
|
|
+ this.options.connectState = true;
|
|
|
+ let userMateData = await this.getUserMetadata(this.userId);
|
|
|
+ if(!userMateData){
|
|
|
+ let user = Parse.User.current();
|
|
|
+ const metadata = [
|
|
|
+ {
|
|
|
+ key: 'nickname',
|
|
|
+ value: user?.get('nickname'),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ key: 'avatar',
|
|
|
+ value:
|
|
|
+ user?.get('avatar') ??
|
|
|
+ 'https://file-cloud.fmode.cn/DXNgcD6zo6/20221202/j6p8kb034039.png',
|
|
|
+ },
|
|
|
+ ];
|
|
|
+ const options = {
|
|
|
+ userId: this.userId,
|
|
|
+ addTimeStamp: true,
|
|
|
+ addUserId: true,
|
|
|
+ };
|
|
|
+ let result = await this.rtmClient.storage.setUserMetadata(
|
|
|
+ metadata,
|
|
|
+ options
|
|
|
+ );
|
|
|
+ console.log(JSON.stringify(result));
|
|
|
+ }
|
|
|
} catch (status) {
|
|
|
console.log(status);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- subscribeMessage(channelId: string, callback?: any) {
|
|
|
- this.rtmClient.subscribe(channelId);
|
|
|
+ logOutRTM() {
|
|
|
+ this.rtmClient.logout().then(() => {
|
|
|
+ console.log('logout success');
|
|
|
+ this.options.connectState = false
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ subscribeMessage(channelName: string, param?: any, callback?: any) {
|
|
|
+ if (this.channelNameList[channelName]) return;
|
|
|
+ const options = {
|
|
|
+ withMessage: param?.message ?? false,
|
|
|
+ withPresence: false,
|
|
|
+ beQuiet: false,
|
|
|
+ withMetadata: false,
|
|
|
+ withLock: false,
|
|
|
+ };
|
|
|
+ this.rtmClient
|
|
|
+ .subscribe(channelName, options)
|
|
|
+ .then((res: any) => {
|
|
|
+ console.log('subscribeMessage', res);
|
|
|
+
|
|
|
+ this.channelNameList[channelName] = true;
|
|
|
+ })
|
|
|
+ .catch((err: any) => {
|
|
|
+ console.error('subscribeMessageErr', err);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ unsubscribeMessage(channelName: string) {
|
|
|
+ this.rtmClient
|
|
|
+ .unsubscribe(channelName)
|
|
|
+ .then((res: any) => {
|
|
|
+ console.log('unsubscribeMessage', res);
|
|
|
+
|
|
|
+ this.channelNameList[channelName] = false;
|
|
|
+ })
|
|
|
+ .catch((err: any) => {
|
|
|
+ console.error('unsubscribeMessage', err);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- showMessage(user: any, msg: string) {
|
|
|
- console.log(user, msg);
|
|
|
- this.messageList.push({
|
|
|
- user,
|
|
|
- msg,
|
|
|
+ async showMessage(param: any) {
|
|
|
+ let userData = await this.getUserMetadata(param.publisher);
|
|
|
+ let is_self = param.publisher == this.userId;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ console.log(userData);
|
|
|
+ let message = JSON.parse(param.message);
|
|
|
+ this.messageMapList[param.channelName].push({
|
|
|
+ is_self: is_self,
|
|
|
+ avatar:
|
|
|
+ userData.avatar.value ??
|
|
|
+ 'https://file-cloud.fmode.cn/DXNgcD6zo6/20221202/j6p8kb034039.png',
|
|
|
+ msg_type: 1,
|
|
|
+ name: userData.nickname.value ?? '未知用户',
|
|
|
+ content: message?.message ?? '',
|
|
|
+ create_time: new Date(param.timeStamp),
|
|
|
+ istoday: true,
|
|
|
+ timeStamp: new Date(param.timeStamp),
|
|
|
});
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ this.pageFun?.();
|
|
|
+ }
|
|
|
+ async getUserMetadata(uid: string) {
|
|
|
+ try {
|
|
|
+ const result = await this.rtmClient.storage.getUserMetadata({
|
|
|
+ userId: uid,
|
|
|
+ });
|
|
|
+ return result.metadata;
|
|
|
+ } catch (status) {
|
|
|
+ console.log(JSON.stringify(status));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- async publishMessage(message: string) {
|
|
|
+ async publishMessage(message: string, channelName: string) {
|
|
|
const payload = { type: 'text', message: message };
|
|
|
const publishMessage = JSON.stringify(payload);
|
|
|
const publishOptions = { channelType: 'MESSAGE' };
|
|
|
try {
|
|
|
const result = await this.rtmClient.publish(
|
|
|
- this.msChannelName,
|
|
|
+ channelName,
|
|
|
publishMessage,
|
|
|
publishOptions
|
|
|
);
|
|
|
- this.showMessage(this.options.userId, publishMessage);
|
|
|
console.log(result);
|
|
|
} catch (status) {
|
|
|
console.log(status);
|