Browse Source

♻️ refactor(rtm): 重构RTM token生成方式

- 移除外部API调用依赖,改用本地token生成方法
- 删除`_baseUrl`常量和`_apiGetAgoraToken`私有方法
- 引入`generateAgoraToken`工具函数处理token生成逻辑
- 优化token生成参数传递方式,直接使用实例属性
yourname 2 tháng trước cách đây
mục cha
commit
4de38378e8
1 tập tin đã thay đổi với 7 bổ sung38 xóa
  1. 7 38
      packages/stt-sdk-core/src/managers/rtm-manager-adapter.ts

+ 7 - 38
packages/stt-sdk-core/src/managers/rtm-manager-adapter.ts

@@ -8,6 +8,7 @@ import type {
   RtmUserInfo,
   RtmChannelMetadata,
 } from '../types'
+import { generateAgoraToken } from '../utils/token-utils'
 
 const { RTM } = AgoraRTM
 const CHANNEL_TYPE: ChannelType = 'MESSAGE'
@@ -23,7 +24,6 @@ export class RtmManagerAdapter extends AGEventEmitter<RtmEventMap> implements IR
   private _appId: string = ''
   private _certificate: string = ''
   private _rtmConfig: RTMConfig = {}
-  private _baseUrl = 'https://service.agora.io/toolbox-overseas'
 
   constructor(appId?: string, certificate?: string) {
     super()
@@ -65,7 +65,12 @@ export class RtmManagerAdapter extends AGEventEmitter<RtmEventMap> implements IR
       this.emit('connecting', { channel, userId })
 
       // 获取RTM token
-      const token = await this._apiGetAgoraToken({ uid: userId, channel })
+      const token = await generateAgoraToken({
+        appId: this._appId,
+        appCertificate: this._certificate,
+        uid: userId,
+        channel,
+      })
       if (token) {
         this._rtmConfig.token = token
       }
@@ -452,40 +457,4 @@ export class RtmManagerAdapter extends AGEventEmitter<RtmEventMap> implements IR
       await this._client?.lock.setLock(this._channel, CHANNEL_TYPE, LOCK_STT)
     }
   }
-
-  private async _apiGetAgoraToken(config: {
-    uid: string | number
-    channel: string
-  }): Promise<string | null> {
-    const { uid, channel } = config
-    const url = `${this._baseUrl}/v2/token/generate`
-    const data = {
-      appId: this._appId,
-      appCertificate: this._certificate,
-      channelName: channel,
-      expire: 7200,
-      src: 'web',
-      types: [1, 2],
-      uid: uid.toString(),
-    }
-
-    try {
-      const response = await fetch(url, {
-        method: 'POST',
-        headers: {
-          'Content-Type': 'application/json',
-        },
-        body: JSON.stringify(data),
-      })
-
-      if (response.ok) {
-        const result = await response.json()
-        return result?.data?.token || null
-      }
-      return null
-    } catch (error) {
-      console.error('Token generation failed:', error)
-      return null
-    }
-  }
 }