123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using LitJson;
- using SC.XR.Unity;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- using UnityEngine.Networking;
- public class HttpsSendLog : MonoBehaviour
- {
- private static HttpsSendLog instance;
- public static HttpsSendLog Instance
- {
- get
- {
- return instance;
- }
- }
-
- private HttpsSendLog()
- {
- }
- private Queue<string> qSendLog;
- private float times;
- Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
- void Awake()
- {
- instance = GetComponent<HttpsSendLog>();
- //http header 的内容
- requestHeader.Add("Content-Type", "application/json");
- }
- private void Start()
- {
- qSendLog = new Queue<string>();
- times = 0;
- // SendLog("Error", "ceshi 11111111111");
-
- }
- public void SendLog(string tag,string message)
- {
- SendHttpsWriteLog sendHLog = new SendHttpsWriteLog();
- sendHLog.level = tag;
- sendHLog.tag = Application.productName;
- sendHLog.message = message;
- string json = JsonMapper.ToJson(sendHLog);
- qSendLog.Enqueue(json);
-
- }
- private void Update()
- {
- times += Time.deltaTime;
- if(times>1&&qSendLog.Count>0)
- {
- PostTest("", qSendLog.Dequeue());
- }
- }
- public void PostTest(string methodName, string jsonString)
- {
- StartCoroutine(PostRequestTest(methodName, jsonString));
- }
- private IEnumerator PostRequestTest(string methodName, string jsonString)
- {
- string url = "https://fat2.qulivr.com/mr-navigator/v1/unity/log";
- Debug.Log(url + jsonString);
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
- {
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
- foreach (var v in requestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.error!=null)
- {
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
- //GameManager.Instance.text2.text = webRequest.error;
- }
- else
- {
- Debug.Log(webRequest.downloadHandler.text);
- }
- }
- }
- }
- public class SendHttpsWriteLog
- {
- public string level { get; set; }
- public string tag { get; set; }
- public string message { get; set; }
- }
|