|
@@ -0,0 +1,180 @@
|
|
|
+import { Injectable } from '@angular/core';
|
|
|
+import { HttpService } from './http.service';
|
|
|
+import { LiveService } from './live.service';
|
|
|
+import * as Parse from 'parse';
|
|
|
+// import AgoraRTM from 'agora-rtmClient';
|
|
|
+declare const AgoraRTM: any;
|
|
|
+@Injectable({
|
|
|
+ providedIn: 'root',
|
|
|
+})
|
|
|
+export class MessageService {
|
|
|
+ rtmClient: any; // RTM实例
|
|
|
+ msChannelName: string = 'global_room'; // 全球频道
|
|
|
+ options: any = {
|
|
|
+ appid: '',
|
|
|
+ userId: Parse.User.current()?.id,
|
|
|
+ token:
|
|
|
+ '007eJxTYGh7sUrIjKd9bl9ncuOq0mvLordY/r5dWv64Y3Wz5uVrNcwKDMmJBqbmJpaGickWliapFkYWBkYWqclp5sbmaSaphhYm+juD0hsCGRlOuFsyMDIwATEjA4jPxZBjkO5maeoU4OMEACaMIUE=',
|
|
|
+ };
|
|
|
+ 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(),
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ ]; // 世界频道消息列表
|
|
|
+ /* 获取token */
|
|
|
+ async getToken(channel: string) {
|
|
|
+ //获取频道token记录
|
|
|
+ let uid = Parse.User.current()?.id;
|
|
|
+ let baseurl = 'https://server.fmode.cn/api/webrtc/build_token';
|
|
|
+ let reqBody = {
|
|
|
+ company: this.liveService.company, // this.aiSer.company,
|
|
|
+ channelName: channel,
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async initRTM() {
|
|
|
+ await this.getToken(this.msChannelName);
|
|
|
+ console.log();
|
|
|
+ this.rtmClient = new AgoraRTM.RTM(this.options.appid, this.options.userId);
|
|
|
+ this.joinReady();
|
|
|
+ await this.loginRTM();
|
|
|
+ // await this.join();
|
|
|
+ this.subscribeMessage(this.msChannelName);
|
|
|
+ }
|
|
|
+ /* 监听频道消息 */
|
|
|
+ joinReady() {
|
|
|
+ this.rtmClient.addEventListener('message', (event: any) => {
|
|
|
+ this.showMessage(event.publisher, event.message);
|
|
|
+ });
|
|
|
+ console.log('initRTM');
|
|
|
+ // Pressence event handler.
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ this.rtmClient.addEventListener('status', (event: any) => {
|
|
|
+ console.log('status event: ', event);
|
|
|
+ // The current connection state.
|
|
|
+ const currentState = event.state;
|
|
|
+ const changeReason = event.reason;
|
|
|
+ // this.showMessage('INFO', JSON.stringify(event));
|
|
|
+ });
|
|
|
+ }
|
|
|
+ /* 加入频道 */
|
|
|
+ async join() {
|
|
|
+ const options = {
|
|
|
+ token: this.options.token,
|
|
|
+ withPresence: true,
|
|
|
+ withLock: true,
|
|
|
+ withMetadata: true,
|
|
|
+ };
|
|
|
+ try {
|
|
|
+ const streamChannel = this.rtmClient.createStreamChannel(
|
|
|
+ this.msChannelName
|
|
|
+ );
|
|
|
+ const result = await streamChannel.join(options);
|
|
|
+ } catch (status) {
|
|
|
+ console.error('join channel failed: ', status);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ async loginRTM() {
|
|
|
+ // console.log(this.options.token);
|
|
|
+ try {
|
|
|
+ const result = await this.rtmClient.login({ token: this.options.token });
|
|
|
+ // console.log(result);
|
|
|
+ } catch (status) {
|
|
|
+ console.log(status);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ subscribeMessage(channelId: string, callback?: any) {
|
|
|
+ this.rtmClient.subscribe(channelId);
|
|
|
+ }
|
|
|
+
|
|
|
+ showMessage(user: any, msg: string) {
|
|
|
+ console.log(user, msg);
|
|
|
+ this.messageList.push({
|
|
|
+ user,
|
|
|
+ msg,
|
|
|
+ });
|
|
|
+ // const inputText = textInput.value;
|
|
|
+ // const newText = document.createTextNode(user + ": " + msg);
|
|
|
+ // const newLine = document.createElement("br");
|
|
|
+ // textDisplay.appendChild(newText);
|
|
|
+ // textDisplay.appendChild(newLine);
|
|
|
+ }
|
|
|
+
|
|
|
+ async publishMessage(message: 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,
|
|
|
+ publishMessage,
|
|
|
+ publishOptions
|
|
|
+ );
|
|
|
+ this.showMessage(this.options.userId, publishMessage);
|
|
|
+ console.log(result);
|
|
|
+ } catch (status) {
|
|
|
+ console.log(status);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|