HttpsSendLog.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using LitJson;
  2. using SC.XR.Unity;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. public class HttpsSendLog : SingletonMono<HttpsSendLog>
  9. {
  10. private Queue<string> qSendLog = new Queue<string>();
  11. private float times;
  12. Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
  13. void Awake()
  14. {
  15. //http header 的内容
  16. requestHeader.Add("Content-Type", "application/json");
  17. }
  18. private void Start()
  19. {
  20. qSendLog = new Queue<string>();
  21. times = 0;
  22. // SendLog("Error", "ceshi 11111111111");
  23. }
  24. public void SendLog(string tag,string message)
  25. {
  26. SendHttpsWriteLog sendHLog = new SendHttpsWriteLog();
  27. sendHLog.level = tag;
  28. sendHLog.tag = Application.productName;
  29. sendHLog.message = message;
  30. string json = JsonMapper.ToJson(sendHLog);
  31. qSendLog.Enqueue(json);
  32. }
  33. private void Update()
  34. {
  35. times += Time.deltaTime;
  36. if(times>1&&qSendLog.Count>0)
  37. {
  38. // PostTest("", qSendLog.Dequeue());
  39. }
  40. }
  41. public void PostTest(string methodName, string jsonString)
  42. {
  43. StartCoroutine(PostRequestTest(methodName, jsonString));
  44. }
  45. private IEnumerator PostRequestTest(string methodName, string jsonString)
  46. {
  47. string url = "https://fat2.qulivr.com/mr-navigator/v1/unity/log";
  48. Debug.Log(url + jsonString);
  49. using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
  50. {
  51. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
  52. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  53. webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  54. foreach (var v in requestHeader)
  55. {
  56. webRequest.SetRequestHeader(v.Key, v.Value);
  57. }
  58. yield return webRequest.SendWebRequest();
  59. if (webRequest.error!=null)
  60. {
  61. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
  62. //GameManager.Instance.text2.text = webRequest.error;
  63. }
  64. else
  65. {
  66. Debug.Log(webRequest.downloadHandler.text);
  67. }
  68. }
  69. }
  70. }
  71. public class SendHttpsWriteLog
  72. {
  73. public string level { get; set; }
  74. public string tag { get; set; }
  75. public string message { get; set; }
  76. }