|
|
@@ -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
|
|
|
- }
|
|
|
- }
|
|
|
}
|