基于接口实现的令牌生成
如果要控制 Vivox 访问令牌 (VAT) 的生成方式,请创建一个实现 Vivox SDK 的 IVivoxTokenProvider
接口的类,并将这个类注册到 VivoxService。
如果通过调用 VivoxService.Instance.SetTokenProvider
来注册 IVivoxTokenProvider 的实现,则每当需要令牌进行登录等操作时,Vivox 都会调用您的实现中的 IVivoxTokenProvider.GetTokenAsync
方法。
创建有效负载所需的信息作为覆盖的 IVivoxTokenProvider.GetTokenAsync
方法的输入提供。此信息在制作 VAT 时使用。
您仍然需要设置服务器端令牌供应,并在覆盖的 IVivoxTokenProvider.GetTokenAsync
方法中获取令牌。
订阅
在登录您的用户之前,您必须使用以下方法注册您的 IVivoxTokenProvider
实现:
VivoxService.Instance.SetTokenProvider(new CustomTokenProvider());
**重要:**订阅流程在 v16.5.0 中发生了变化。对于 16.5.0 之前的 Unity Vivox SDK 版本,您必须在初始化 Vivox SDK 之前注册您的 IVivoxTokenProvider
实现。
获取令牌
应使用覆盖的方法中提供的所有参数(即使有些参数为空)创建有效负载,并将有效负载发送到您的安全服务器以生成您的 Vivox 访问令牌。最佳做法是发送所有参数;仅返回有效负载所需的内容。使用有效负载作为 GetTokenAsync
方法的输入。
对于某些令牌,不同的参数将为空。例如,登录令牌的 targetUserUri 和 channelUri 将为空。这是预期的行为。
以下是一个令牌生成示例。
public 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)
{
// Implement token fetching logic here.
// The method parameters contain the necessary information for crafting the request payload.
// This will be called whenever a token is needed for a Vivox action
}
}
如需了解有关此特定流程的更多详细信息,请参阅使用 Vivox 访问令牌登录。
要了解有关生成服务器端令牌的更多信息,请参阅在安全服务器上生成令牌。