1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using Blue;
- using LitJson;
- using UnityEngine;
- using UnityEngine.Networking;
- public interface ISendLogService : IService
- {
- void SendLog(string tag, string message);
- }
- public class SendLogService : ISendLogService
- {
- private Queue<string> sendLogMessage;
- private Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
- private BindableProperty<int> MessageCount { get; set; } = new BindableProperty<int> { Value = 0 };
- public void OnInit()
- {
- requestHeader.Add("Content-Type", "application/json");
- sendLogMessage = new Queue<string>();
- MessageCount.Subscribe(newInt=>
- {
- PostMessage("", sendLogMessage.Dequeue());
- });
- }
- public void SendLog(string level, string message)
- {
- SendLogInfo sendHLog = new SendLogInfo();
- sendHLog.level = level;
- sendHLog.tag = Application.productName;
- sendHLog.message = message;
- string json = JsonMapper.ToJson(sendHLog);
- sendLogMessage.Enqueue(json);
- MessageCount.Value += 1;
- }
- private void PostMessage(string methodName, string jsonString)
- {
- CoroutineExtensionUtility.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 SendLogInfo
- {
- public string level { get; set; }
- public string tag { get; set; }
- public string message { get; set; }
- }
|