NetWorkManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using LitJson;
  5. using System;
  6. using rtc;
  7. using UnityEngine.SceneManagement;
  8. public class NetWorkManager : MonoBehaviour {
  9. private List<Network> queue;
  10. private static bool init;
  11. public static NetWorkManager instance;
  12. void Awake()
  13. {
  14. if (!init) {
  15. init = true;
  16. instance = this;
  17. queue = new List<Network> ();
  18. DontDestroyOnLoad (this.gameObject);
  19. } else {
  20. Destroy (this.gameObject);
  21. return;
  22. }
  23. }
  24. // Use this for initialization
  25. void Start () {
  26. // HotPool.getHotList ();
  27. // List<int> i = new List<int> ();
  28. // i.Add (1);
  29. // i.Add (2);
  30. // string s = JsonUtility.ToJson (i);
  31. // Debug.Log (s);
  32. }
  33. void checkQueue()
  34. {
  35. Debug.Log ("networkQueueCount: " + queue.Count);
  36. if (queue.Count == 0) {
  37. //Debug.Log ("networkQueue: " + 0);
  38. return;
  39. }
  40. if (queue [0].active) {
  41. //Debug.Log ("networkActive: " + queue[0].action);
  42. return;
  43. }
  44. StartCoroutine (post(queue[0]));
  45. }
  46. IEnumerator post(Network net)
  47. {
  48. yield return null;
  49. /*
  50. //Debug.Log ("networkPost");
  51. net.active = true;
  52. string url = net.action;
  53. // string url = NetworkConfiguration.url + "?s=index/index1/index";
  54. Dictionary<string, string> form = net.data;
  55. //UserInfo.Token = "f8009f01eb9b17e24aa57bf86aaadda9";
  56. //form.AddField ("user_token",UserInfo.Token);
  57. Debug.Log ("token===>"+UserInfo.Token);
  58. //form.AddField ("app_id", UserInfo.AppID);
  59. // form.AddField ("param", net.data);
  60. WWW www = new WWW (url,form);
  61. Debug.Log("url: " + www.url);
  62. Debug.Log("param: " + net.data);
  63. yield return www;//等待Web服务器的反应
  64. string result = string.Empty;
  65. if (www.error != null) {
  66. result = www.error;
  67. Debug.Log ("networkError: " + result);
  68. postError ("1009");
  69. } else {
  70. result = www.text;
  71. Debug.Log ("networkSuccess: " + result);
  72. //txt.text = result;
  73. postSuccess (result);
  74. }
  75. queue.RemoveAt (0);
  76. checkQueue ();*/
  77. }
  78. private TextMesh txt;
  79. public void getNetTexture(string action,WWWForm data,Action<Material> success = null)
  80. {
  81. StartCoroutine (LoadTextureFromLocal(action,data,success));
  82. }
  83. //读取纹理
  84. IEnumerator LoadTextureFromLocal(string action,WWWForm data=null,Action<Material> success = null)
  85. {
  86. Debug.Log ("tupian123................"+action);
  87. WWW w;
  88. if(data!=null)
  89. w = new WWW (action,data);
  90. else
  91. w = new WWW (action);
  92. yield return w;
  93. if (w.isDone) {
  94. Material material = new Material (Shader.Find ("Unlit/Texture"));
  95. Debug.Log ("tupian321................");
  96. material.mainTexture = w.texture;
  97. if(success!=null)
  98. success (material);
  99. }
  100. }
  101. void postSuccess(string postData)
  102. {
  103. JsonData data = null;
  104. string code = "";
  105. try
  106. {
  107. data = JsonMapper.ToObject (postData);
  108. //UserInfo.Token = data["token"].ToString();
  109. code = data ["code"].ToString();
  110. }
  111. catch(Exception e)
  112. {
  113. Debug.Log (e.Data);
  114. postError ("1009");
  115. }
  116. switch (code) {
  117. case "200":
  118. queue [0].scuess (data);
  119. break;
  120. default:
  121. Debug.Log("22222222222222");
  122. if (data ["message"].ToString () == "vaild account") {
  123. PlayerPrefs.SetString (UserInfo.Account,"");
  124. }
  125. queue [0].failed (postData);
  126. break;
  127. }
  128. }
  129. void postError(string errorData)
  130. {
  131. queue [0].error (errorData);
  132. }
  133. /// <summary>
  134. /// Adds the network.
  135. /// </summary>
  136. /// <param name="action">Action.</param>
  137. /// <param name="data">Data.</param>
  138. /// <param name="type">Type.</param>
  139. /// <param name="successFun">Success fun.</param>
  140. /// <param name="failedFun">Failed fun.</param>
  141. public void addNetwork(string action, JsonData data,Action<JsonData> successFun = null ,Action<string> failedFun = null)
  142. {
  143. Network net = new Network ();
  144. net.init (action,data,successFun,failedFun);
  145. queue.Add (net);
  146. checkQueue ();
  147. }
  148. public void netWorkComplete()
  149. {
  150. netWorkComplete ();
  151. }
  152. public static void TextureLoad(string session,Action<Material> success = null)
  153. {
  154. WWWForm data = new WWWForm ();
  155. data.AddField("session_id", session);
  156. string action = NetworkConfiguration.get_code_png_url;
  157. NetWorkManager.instance.getNetTexture (action,data,success);
  158. }
  159. }