Last active
August 10, 2020 16:36
-
-
Save digitallysavvy/51f2e6903a2d186b724ca634861793f9 to your computer and use it in GitHub Desktop.
a function that generates an RTC token from the given inputs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func generateRtcToken(channelName, uidStr, tokentype string, role rtctokenbuilder.Role, expireTimestamp uint32) (rtcToken string, err error) { | |
if tokentype == "userAccount" { | |
log.Printf("Building Token with userAccount: %s\n", uidStr) | |
rtcToken, err = rtctokenbuilder.BuildTokenWithUserAccount(appID, appCertificate, channelName, uidStr, role, expireTimestamp) | |
return rtcToken, err | |
} else if tokentype == "uid" { | |
uid64, parseErr := strconv.ParseUint(uidStr, 10, 64) | |
// check if conversion fails | |
if parseErr != nil { | |
err = fmt.Errorf("failed to parse uidStr: %s, to uint causing error: %s", uidStr, parseErr) | |
return "", err | |
} | |
uid := uint32(uid64) // convert uid from uint64 to uint 32 | |
log.Printf("Building Token with uid: %d\n", uid) | |
rtcToken, err = rtctokenbuilder.BuildTokenWithUID(appID, appCertificate, channelName, uid, role, expireTimestamp) | |
return rtcToken, err | |
} else { | |
err = fmt.Errorf("failed to generate RTC token for Unknown Tokentype: %s", tokentype) | |
log.Println(err) | |
return "", err | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment