|
@@ -30,7 +30,7 @@ export const useAgoraSTT = (): UseAgoraSTTResult => {
|
|
|
updateState({ error });
|
|
updateState({ error });
|
|
|
}, [updateState]);
|
|
}, [updateState]);
|
|
|
|
|
|
|
|
- const fetchDynamicToken = useCallback(async (type: 'rtc' | 'rtm', channel?: string, userId?: string): Promise<string> => {
|
|
|
|
|
|
|
+ const fetchAgoraConfigAndToken = useCallback(async (type: 'rtc' | 'rtm', channel?: string, userId?: string): Promise<{ token: string; config: any }> => {
|
|
|
try {
|
|
try {
|
|
|
const query: any = { type };
|
|
const query: any = { type };
|
|
|
|
|
|
|
@@ -52,9 +52,18 @@ export const useAgoraSTT = (): UseAgoraSTTResult => {
|
|
|
throw new Error('Invalid token response format');
|
|
throw new Error('Invalid token response format');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return data.token;
|
|
|
|
|
|
|
+ // 现在API返回包含Token和配置常量的完整响应
|
|
|
|
|
+ return {
|
|
|
|
|
+ token: data.token,
|
|
|
|
|
+ config: {
|
|
|
|
|
+ appId: data.appId,
|
|
|
|
|
+ sttJoinUrl: data.sttJoinUrl,
|
|
|
|
|
+ sttWsUrl: data.sttWsUrl,
|
|
|
|
|
+ defaultChannel: data.defaultChannel
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
- throw new Error(`Failed to fetch dynamic token: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
|
|
|
|
|
+ throw new Error(`Failed to fetch dynamic token and config: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
|
}
|
|
}
|
|
|
}, []);
|
|
}, []);
|
|
|
|
|
|
|
@@ -65,15 +74,9 @@ export const useAgoraSTT = (): UseAgoraSTTResult => {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const agoraConfig = getAgoraConfig();
|
|
|
|
|
- const validationError = validateAgoraConfig(agoraConfig);
|
|
|
|
|
-
|
|
|
|
|
- if (validationError) {
|
|
|
|
|
- setError(validationError);
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- config.current = agoraConfig;
|
|
|
|
|
|
|
+ // 现在配置通过Token API获取,不再需要验证本地配置
|
|
|
|
|
+ // 使用默认配置作为占位符
|
|
|
|
|
+ config.current = getAgoraConfig();
|
|
|
return true;
|
|
return true;
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
setError('Failed to initialize Agora configuration');
|
|
setError('Failed to initialize Agora configuration');
|
|
@@ -89,16 +92,25 @@ export const useAgoraSTT = (): UseAgoraSTTResult => {
|
|
|
try {
|
|
try {
|
|
|
updateState({ error: null, isConnecting: true });
|
|
updateState({ error: null, isConnecting: true });
|
|
|
|
|
|
|
|
- // 动态获取RTC Token
|
|
|
|
|
- const dynamicToken = await fetchDynamicToken('rtc', config.current!.channel);
|
|
|
|
|
- currentToken.current = dynamicToken;
|
|
|
|
|
|
|
+ // 统一获取Token和配置常量
|
|
|
|
|
+ const { token, config: dynamicConfig } = await fetchAgoraConfigAndToken('rtc', config.current!.channel);
|
|
|
|
|
+ currentToken.current = token;
|
|
|
|
|
+
|
|
|
|
|
+ // 更新配置为API返回的配置常量
|
|
|
|
|
+ config.current = {
|
|
|
|
|
+ ...config.current,
|
|
|
|
|
+ appId: dynamicConfig.appId,
|
|
|
|
|
+ sttJoinUrl: dynamicConfig.sttJoinUrl,
|
|
|
|
|
+ sttWsUrl: dynamicConfig.sttWsUrl,
|
|
|
|
|
+ channel: dynamicConfig.defaultChannel
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
// 模拟Agora STT加入频道API调用
|
|
// 模拟Agora STT加入频道API调用
|
|
|
const joinResponse = await fetch(config.current!.sttJoinUrl, {
|
|
const joinResponse = await fetch(config.current!.sttJoinUrl, {
|
|
|
method: 'POST',
|
|
method: 'POST',
|
|
|
headers: {
|
|
headers: {
|
|
|
'Content-Type': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
|
- 'Authorization': `Bearer ${dynamicToken}`
|
|
|
|
|
|
|
+ 'Authorization': `Bearer ${token}`
|
|
|
},
|
|
},
|
|
|
body: JSON.stringify({
|
|
body: JSON.stringify({
|
|
|
appId: config.current!.appId,
|
|
appId: config.current!.appId,
|
|
@@ -167,7 +179,7 @@ export const useAgoraSTT = (): UseAgoraSTTResult => {
|
|
|
setError('Failed to join Agora channel: ' + (error instanceof Error ? error.message : 'Unknown error'));
|
|
setError('Failed to join Agora channel: ' + (error instanceof Error ? error.message : 'Unknown error'));
|
|
|
updateState({ isConnecting: false });
|
|
updateState({ isConnecting: false });
|
|
|
}
|
|
}
|
|
|
- }, [initializeConfig, updateState, setError, fetchDynamicToken]);
|
|
|
|
|
|
|
+ }, [initializeConfig, updateState, setError, fetchAgoraConfigAndToken]);
|
|
|
|
|
|
|
|
const leaveChannel = useCallback((): void => {
|
|
const leaveChannel = useCallback((): void => {
|
|
|
if (wsConnection.current) {
|
|
if (wsConnection.current) {
|