RequestToken.cs 844 B

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. [Serializable]
  6. public class TokenObject {
  7. public string rtcToken;
  8. }
  9. namespace Agora.Util
  10. {
  11. public static class HelperClass
  12. {
  13. public static IEnumerator FetchToken(
  14. string url, string channel, int userId, Action<string> callback = null
  15. ) {
  16. UnityWebRequest request = UnityWebRequest.Get(string.Format(
  17. "{0}/rtc/{1}/publisher/uid/{2}/", url, channel, userId
  18. ));
  19. yield return request.SendWebRequest();
  20. if (request.isNetworkError || request.isHttpError) {
  21. Debug.Log(request.error);
  22. callback(null);
  23. yield break;
  24. }
  25. TokenObject tokenInfo = JsonUtility.FromJson<TokenObject>(
  26. request.downloadHandler.text
  27. );
  28. callback(tokenInfo.rtcToken);
  29. }
  30. }
  31. }