GameLogger.cs 1.1 KB

12345678910111213141516171819202122232425262728
  1. using System.Threading.Tasks;
  2. using UnityEngine;
  3. using UnityEngine.Networking;
  4. namespace Web3Unity.Scripts.Library.ETHEREUEM.WebGL
  5. {
  6. public class GameLogger
  7. {
  8. private const string loggingUrl = "https://game-api-stg.chainsafe.io/logging/logEvent";
  9. public static async Task<string> Log(string _chain, string _network, object _data)
  10. {
  11. using var webRequest = new UnityWebRequest(loggingUrl, "POST");
  12. webRequest.timeout = -1;
  13. var bodyRaw = System.Text.Encoding.UTF8.GetBytes($"chain={_chain}&network={_network}&gameData={_data}");
  14. webRequest.uploadHandler = new UploadHandlerRaw(bodyRaw);
  15. webRequest.downloadHandler = new DownloadHandlerBuffer();
  16. webRequest.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  17. await webRequest.SendWebRequest();
  18. return webRequest.result switch
  19. {
  20. UnityWebRequest.Result.ProtocolError => webRequest.error,
  21. _ => webRequest.downloadHandler.text
  22. };
  23. }
  24. }
  25. }