将 Vivox 访问令牌与 UAS 结合使用
Follow this workflow to use Vivox Access Tokens together with Unity Authentication Services.
阅读时间4 分钟最后更新于 13 天前
为了完全控制哪些玩家可以访问哪些频道,您可以通过自己的后端生成 Vivox 访问令牌 (VAT)。
先决条件:
- 设置 Unity Cloud 项目
- 确保您拥有 Project ID、环境 ID 和环境名称。
- 启用 UAS 自定义 ID 提供商
- 确保您在此设置中拥有服务帐户凭据。

创建 VAT
- 每当需要 VAT 时,Vivox SDK 都会使用自定义令牌提供商实现从后端请求 VAT。
- 在 方法内请求 VAT 时,向后端的请求中添加
GetTokenAsync。accessToken - 在后端从令牌中验证并提取所需的信息,并创建 Vivox 访问令牌以将其发送回客户端。
客户端设置
在游戏客户端中,创建一个实现 Vivox SDKIVivoxTokenProviderVivoxService.Instance.SetTokenProviderIVivoxTokenProvider.GetTokenAsyncIVivoxTokenProvider.GetTokenAsyncIVivoxTokenProvider.GetTokenAsync订阅
在登录您的玩家之前,您必须使用以下方法注册您的IVivoxTokenProviderVivoxService.Instance.SetTokenProvider(new CustomTokenProvider());
获取令牌
应使用覆盖的方法中提供的所有参数(即使有些参数为空)创建有效负载,并将有效负载发送到您的安全服务器以生成您的 Vivox 访问令牌。 最佳做法是发送所有参数;仅返回有效负载所需的内容。使用有效负载作为GetTokenAsynctargetUserUrichannelUripublic class VoiceManager : MonoBehaviour{ async void Start() { // Must be done before any other Vivox action otherwise tokens will not be generated properly. VivoxService.Instance.SetTokenProvider(new VivoxTokenProvider()); await UnityServices.InitializeAsync(); await VivoxService.Instance.InitializeAsync(); }}class VivoxTokenProvider : IVivoxTokenProvider{ public Task<string> GetTokenAsync(string issuer = null, TimeSpan? expiration = null, string targetUserUri = null, string action = null, string channelUri = null, string fromUserUri = null, string realm = null) { if (!AuthenticationService.Instance.SessionTokenExists) { // Player not logged in! } var accessToken = AuthenticationService.Instance.AccessToken // Implement token fetching logic here. // The method parameters together with the accessToken from the AuthenticationService contain the necessary information for crafting the request payload. // This will be called whenever a token is needed for a Vivox action }}
服务器端设置
您的后端服务需要公开一个 API,让客户端可以在以上的GetTokenAsyncsip:.issuer.unity_player_id.unity_environment_id.@domain.vivox.com
issuerunity_player_idunity_environment_idGetTokenAsyncaccessTokensubaudaud这样就可以创建 VAT 并将其返回到客户端。{ "header": { "alg": "RS256", "kid": "<id>", "typ": "JWT" }, "payload": { "aud": [ "upid:{{unity_project_id}}", "envName:{{environment_name}}", "envId:{{unity_environment_id}}" // unity environment id ], "exp": 1617677595, // expires at "iat": 1617673872, // issued at "nbf": 1617673872, // not valid before "sub": "{{unity_player_id}}", // UAS player ID "project_id": "{{unity_project_id}}" // unity project id }}