diff --git a/src/page/Meeting/index.tsx b/src/page/Meeting/index.tsx index 759553f..c9b2575 100644 --- a/src/page/Meeting/index.tsx +++ b/src/page/Meeting/index.tsx @@ -25,6 +25,7 @@ import { getKeyOpenChildWindow, setKeyOpenChildWindow } from '@/utils/package/pu import MeetingDisconnected from '@/components/MeetingDisconnected'; import SingIn from '@/components/SingIn'; import UserName from '@/components/UserName'; +import { GetRoomRtcToken } from '@/api/Home/Index'; const { setTimeout, setInterval, clearTimeout, clearInterval } = require('timers'); const { confirm } = Modal; const { exec } = require('child_process'); @@ -1155,6 +1156,16 @@ const Meeting: React.FC = () => { key: 'cameraTemporarily' }); } + }, + onTokenPrivilegeWillExpire: async (connection: RtcConnection, _token: string) => { + await GetRoomRtcToken(connection.channelId || '').then(res => { + if (res.code === 200) { + agora.refreshToken({ + token: res.data.token, + connection, + }) + } + }) } }) if (state.enableCamera) { diff --git a/src/utils/package/agora.ts b/src/utils/package/agora.ts index 90b9860..7a39177 100644 --- a/src/utils/package/agora.ts +++ b/src/utils/package/agora.ts @@ -153,7 +153,7 @@ export const agora = { }, 1000); }, // 事件回调 - registerEventHandler: ({ onJoinChannelSuccess, onUserJoined, onUserOffline, onAudioVolumeIndication, onNetworkQuality, onRtcStats, onConnectionStateChanged, onLocalVideoStateChanged, onConnectionLost }: any) => { + registerEventHandler: ({ onJoinChannelSuccess, onUserJoined, onUserOffline, onAudioVolumeIndication, onNetworkQuality, onRtcStats, onConnectionStateChanged, onLocalVideoStateChanged, onConnectionLost, onTokenPrivilegeWillExpire }: any) => { rtcEngine.registerEventHandler({ // 监听本地用户加入频道事件 onJoinChannelSuccess: async (connection: RtcConnection, elapsed: number) => { @@ -202,9 +202,19 @@ export const agora = { // 网络连接中断,且 SDK 无法在 10 秒内连接服务器回调。 onConnectionLost: (_connection: RtcConnection) => { onConnectionLost?.() + }, + // Token 即将在 30s 内过期回调。 + onTokenPrivilegeWillExpire: (connection: RtcConnection, token: string) => { + onTokenPrivilegeWillExpire?.(connection, token) } }); }, + // 刷新token + refreshToken: async (data: any) => { + await rtcEngine.updateChannelMediaOptionsEx({ + token: data.token, + }, data.connection); + }, // 获取视图模式 getRrenderMode: (uid: number) => { if (String(uid).length === 9) {