2
0
Эх сурвалжийг харах

🐛 fix(agora): 修复RTM连接和Token生成问题

- 修改默认频道名称为"123"以匹配测试环境
- 移除冗余的fetchAgoraConfig调用,优化连接流程
- 修复错误提示信息,使错误定位更准确
- 统一使用primaryCert替代appSecret命名,与Agora官方术语保持一致
- 直接使用API返回的token,避免重复调用导致的性能问题
yourname 4 сар өмнө
parent
commit
d5259ab36b

+ 1 - 1
src/client/admin/components/agora-stt/AgoraSTTComponent.tsx

@@ -30,7 +30,7 @@ export const AgoraSTTComponent: React.FC<AgoraSTTComponentProps> = ({
   const handleJoinChannel = async () => {
     try {
       const userId = Date.now().toString();
-      const channel = 'default-channel';
+      const channel = '123';
       const userName = 'User';
       await joinChannel(userId, channel, userName);
     } catch (_error) {

+ 5 - 8
src/client/admin/components/agora-stt/manager/rtm/rtm.ts

@@ -1,5 +1,5 @@
 import AgoraRTM, { ChannelType, RTMClient, RTMConfig, MetadataItem } from "agora-rtm-sdk"
-import { mapToArray, isString, apiGetAgoraToken, getDefaultLanguageSelect, fetchAgoraConfig } from "../../common"
+import { mapToArray, isString, getDefaultLanguageSelect } from "../../common"
 import { AGEventEmitter } from "../events"
 import {
   RtmEvents,
@@ -38,17 +38,14 @@ export class RtmManager extends AGEventEmitter<RtmEvents> {
     this.userName = userName
     this.channel = channel
 
-    // 获取配置
-    await fetchAgoraConfig()
-
-    // 从配置中获取appId
+    // 获取RTM配置和Token
     const { agoraClient } = await import("@/client/api")
     const response = await agoraClient.token.$get({
       query: { type: 'rtm', channel, userId }
     })
 
     if (!response.ok) {
-      throw new Error('配置获取失败')
+      throw new Error('RTM配置获取失败')
     }
 
     const data = await response.json()
@@ -57,8 +54,8 @@ export class RtmManager extends AGEventEmitter<RtmEvents> {
       this.client = new RTM(data.appId, userId, this.rtmConfig)
     }
     this._listenRtmEvents()
-    const token = await apiGetAgoraToken({ channel: this.channel, uid: this.userId })
-    await this.client.login({ token })
+    // 直接使用API返回的token,避免重复调用
+    await this.client.login({ token: data.token })
     this.joined = true
     // subscribe message channel
     await this.client.subscribe(channel, {

+ 8 - 7
src/server/modules/agora/agora-token.service.ts

@@ -3,17 +3,18 @@ const {RtcTokenBuilder, RtmTokenBuilder, RtcRole} = pkg;
 
 export class AgoraTokenService {
   private appId: string
-  private appSecret: string
+  private primaryCert: string
   private tokenExpiry: number
 
   constructor() {
     this.appId = process.env.AGORA_APP_ID || ''
-    this.appSecret = process.env.AGORA_APP_SECRET || ''
+    // this.primaryCert = process.env.AGORA_APP_SECRET || ''
+    this.primaryCert = process.env.AGORA_PRIMARY_CERT || ''
     this.tokenExpiry = parseInt(process.env.AGORA_TOKEN_EXPIRY || '3600')
 
     // 在生产环境中检查配置,测试环境中允许为空
-    if (process.env.NODE_ENV === 'production' && (!this.appId || !this.appSecret)) {
-      throw new Error('Agora配置缺失:请设置AGORA_APP_ID和AGORA_APP_SECRET环境变量')
+    if (process.env.NODE_ENV === 'production' && (!this.appId || !this.primaryCert)) {
+      throw new Error('Agora配置缺失:请设置AGORA_APP_ID和AGORA_PRIMARY_CERT环境变量')
     }
   }
 
@@ -27,7 +28,7 @@ export class AgoraTokenService {
 
       console.debug('生成RTC Token参数:', {
         appId: this.appId ? '已设置' : '未设置',
-        appSecret: this.appSecret ? '已设置' : '未设置',
+        primaryCert: this.primaryCert ? '已设置' : '未设置',
         channelName,
         userId,
         privilegeExpiredTs
@@ -35,7 +36,7 @@ export class AgoraTokenService {
 
       const token = RtcTokenBuilder.buildTokenWithUid(
         this.appId,
-        this.appSecret,
+        this.primaryCert,
         channelName,
         typeof userId === 'number' ? userId : parseInt(userId),
         RtcRole.PUBLISHER,
@@ -62,7 +63,7 @@ export class AgoraTokenService {
 
       const token = RtmTokenBuilder.buildToken(
         this.appId,
-        this.appSecret,
+        this.primaryCert,
         userId,
         privilegeExpiredTs
       )