HttpRequest.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using System.Collections;
  4. using LitJson;
  5. public class HttpRequestEvent : UnityEvent<bool, JsonData>{}
  6. public class HttpRequest
  7. {
  8. protected const string resCodeKey = "c";
  9. protected const string resDataKey = "d";
  10. protected string m;
  11. protected string c;
  12. protected string a;
  13. public HttpRequestEvent ResultEvent;
  14. private bool showPending;
  15. public HttpRequest()
  16. {
  17. ResultEvent = new HttpRequestEvent();
  18. }
  19. protected void Send (URLRequestData data, bool showPending = false)
  20. {
  21. this.showPending = showPending;
  22. if(showPending)
  23. ProgressPanel.Show ();
  24. URLRequest.CreateURLRequest (GetURL(), data, Received, URLRequest.Method.POST);
  25. }
  26. protected void Received (JsonData data)
  27. {
  28. if(showPending)
  29. ProgressPanel.Hide();
  30. if (JsonUtil.ContainKey (data, resCodeKey)) {
  31. int code = JsonUtil.ToInt (data [resCodeKey]);
  32. if (code == 0) {
  33. ResultEvent.Invoke (true, data [resDataKey]);
  34. } else {
  35. Debuger.LogError ("Error code : " + code);
  36. ResultEvent.Invoke (false, data [resDataKey]);
  37. }
  38. } else {
  39. Debuger.LogError ("invilid data : " + data);
  40. ResultEvent.Invoke (false, null);
  41. }
  42. ResultEvent.RemoveAllListeners ();
  43. }
  44. protected string GetURL()
  45. {
  46. return Config.URL_ADDRESS + "game/" + Config.VERSION_LABEL + "/index.php?m=" + m + "&c=" + c + "&a=" + a;
  47. }
  48. }