import { RtcRole , RtcTokenBuilder , RtmRole , RtmTokenBuilder } from "agora-access-token" ; import { Agora } from "../../constants/Process" ; import { RedisKey } from "../../utils/Redis" ; import RedisService from "../../thirdPartyService/RedisService" ; const generateRTCToken = ( title , uid ) : string => { return RtcTokenBuilder . buildTokenWithUid ( Agora . APP_ID , Agora . APP_CERTIFICATE , title , uid , RtcRole . PUBLISHER , Math . floor ( Date . now ( ) / 1000 ) + 60 * 60 * 24 ) ; } ; const generateRTMToken = ( uid ) : string => { return RtmTokenBuilder . buildToken ( Agora . APP_ID , Agora . APP_CERTIFICATE , uid , RtmRole . Rtm_User , 0 ) ; } ; export const getRTCToken = async ( roomUUID , rtcUID ) : Promise < string > => { const rtcKey = RedisKey . agoraRTCRoomUserToken ( roomUUID , rtcUID ) ; let rtcToken = await RedisService . get ( rtcKey ) ; if ( rtcToken === null ) { rtcToken = generateRTCToken ( roomUUID , rtcUID ) ; await RedisService . set ( rtcKey , rtcToken , 60 * 60 * 24 - 60 ) ; } return rtcToken ; } ; export const getRTMToken = async ( userUUID ) : Promise < string > => { const rtmKey = RedisKey . agoraRTMUserToken ( userUUID ) ; let rtmToken = await RedisService . get ( rtmKey ) ; if ( rtmToken === null ) { rtmToken = generateRTMToken ( userUUID ) ; await RedisService . set ( rtmKey , rtmToken , 60 * 60 * 24 - 60 ) ; } return rtmToken ; } ;