|
@@ -21,13 +21,18 @@ export class RtmManagerAdapter extends AGEventEmitter<RtmEventMap> implements IR
|
|
|
private _userMap: Map<string, RtmUserInfo> = new Map()
|
|
private _userMap: Map<string, RtmUserInfo> = new Map()
|
|
|
private _client?: RTMClient
|
|
private _client?: RTMClient
|
|
|
private _appId: string = ''
|
|
private _appId: string = ''
|
|
|
|
|
+ private _certificate: string = ''
|
|
|
private _rtmConfig: RTMConfig = {}
|
|
private _rtmConfig: RTMConfig = {}
|
|
|
|
|
+ private _baseUrl = 'https://service.agora.io/toolbox-overseas'
|
|
|
|
|
|
|
|
- constructor(appId?: string) {
|
|
|
|
|
|
|
+ constructor(appId?: string, certificate?: string) {
|
|
|
super()
|
|
super()
|
|
|
if (appId) {
|
|
if (appId) {
|
|
|
this._appId = appId
|
|
this._appId = appId
|
|
|
}
|
|
}
|
|
|
|
|
+ if (certificate) {
|
|
|
|
|
+ this._certificate = certificate
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async join(config: RtmManagerConfig): Promise<void> {
|
|
async join(config: RtmManagerConfig): Promise<void> {
|
|
@@ -46,12 +51,25 @@ export class RtmManagerAdapter extends AGEventEmitter<RtmEventMap> implements IR
|
|
|
throw new SttError('APP_ID_REQUIRED', 'App ID is required for RTM connection')
|
|
throw new SttError('APP_ID_REQUIRED', 'App ID is required for RTM connection')
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if (!this._certificate) {
|
|
|
|
|
+ throw new SttError(
|
|
|
|
|
+ 'CERTIFICATE_REQUIRED',
|
|
|
|
|
+ 'Certificate is required for RTM token generation'
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
this._userId = userId
|
|
this._userId = userId
|
|
|
this._channel = channel
|
|
this._channel = channel
|
|
|
this._config = config
|
|
this._config = config
|
|
|
|
|
|
|
|
this.emit('connecting', { channel, userId })
|
|
this.emit('connecting', { channel, userId })
|
|
|
|
|
|
|
|
|
|
+ // 获取RTM token
|
|
|
|
|
+ const token = await this._apiGetAgoraToken({ uid: userId, channel })
|
|
|
|
|
+ if (token) {
|
|
|
|
|
+ this._rtmConfig.token = token
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 创建RTM客户端
|
|
// 创建RTM客户端
|
|
|
this._client = new RTM(this._appId, userId, this._rtmConfig)
|
|
this._client = new RTM(this._appId, userId, this._rtmConfig)
|
|
|
|
|
|
|
@@ -434,4 +452,40 @@ export class RtmManagerAdapter extends AGEventEmitter<RtmEventMap> implements IR
|
|
|
await this._client?.lock.setLock(this._channel, CHANNEL_TYPE, LOCK_STT)
|
|
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
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|