1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using UnityEngine;
- using UnityEngine.Events;
- using System.Collections;
- using LitJson;
- public class HttpRequestEvent : UnityEvent<bool, JsonData>{}
- public class HttpRequest
- {
- protected const string resCodeKey = "c";
- protected const string resDataKey = "d";
- protected string m;
- protected string c;
- protected string a;
- public HttpRequestEvent ResultEvent;
- private bool showPending;
- public HttpRequest()
- {
- ResultEvent = new HttpRequestEvent();
- }
- protected void Send (URLRequestData data, bool showPending = false)
- {
- this.showPending = showPending;
- if(showPending)
- ProgressPanel.Show ();
- URLRequest.CreateURLRequest (GetURL(), data, Received, URLRequest.Method.POST);
- }
- protected void Received (JsonData data)
- {
- if(showPending)
- ProgressPanel.Hide();
-
- if (JsonUtil.ContainKey (data, resCodeKey)) {
- int code = JsonUtil.ToInt (data [resCodeKey]);
- if (code == 0) {
- ResultEvent.Invoke (true, data [resDataKey]);
- } else {
- Debuger.LogError ("Error code : " + code);
- ResultEvent.Invoke (false, data [resDataKey]);
- }
- } else {
- Debuger.LogError ("invilid data : " + data);
- ResultEvent.Invoke (false, null);
- }
- ResultEvent.RemoveAllListeners ();
- }
- protected string GetURL()
- {
- return Config.URL_ADDRESS + "game/" + Config.VERSION_LABEL + "/index.php?m=" + m + "&c=" + c + "&a=" + a;
- }
- }
|