Unity之与nodejs服务器通信项目代码
Unity与nodejs图片服上传下载图片等的项目代码,分享如下:
1 using UnityEngine; 2 using System.Collections; 3 using System.Text; 4 using System.Collections.Generic; 5 using System; 6 using ClientData; 7 using UnityEngine.Networking; 8 namespace Common 9 { 10 public class WebServer : MonoBehaviour 11 { 12 private static WebServer m_instance; 13 public static WebServer instance 14 { 15 get 16 { 17 if (m_instance == null) 18 { 19 GameObject go = new GameObject("WebServer"); 20 m_instance = go.AddComponent<WebServer>(); 21 } 22 return m_instance; 23 } 24 } 25 26 string AddressPhotoUrl 27 { 28 get 29 { 30 return PackageConfig.Singleton.GetPackageInfo().photoServer; 31 } 32 } 33 string AddressDataUrl 34 { 35 get 36 { 37 return PackageConfig.Singleton.GetFtpUrl(); 38 } 39 } 40 41 public void Post(string page, string method, Hashtable data, Action<WWW> result) 42 { 43 WWWForm form = new WWWForm(); 44 foreach (string key in data.Keys) 45 { 46 form.AddField(key, data[key].ToString()); 47 } 48 string url = AddressPhotoUrl + (String.IsNullOrEmpty(page) ? "" : "/" + page) + (String.IsNullOrEmpty(method) ? "" : "/" + method); 49 StartCoroutine(PostWWW(url, form, result)); 50 } 51 52 public void PostFile(string page, string method, Hashtable data, byte[] file, Action<WWW> result) 53 { 54 WWWForm form = new WWWForm(); 55 foreach (string key in data.Keys) 56 { 57 form.AddField(key, data[key].ToString()); 58 } 59 form.AddBinaryData("file", file); 60 string url = AddressPhotoUrl + (String.IsNullOrEmpty(page) ? "" : "/" + page) + (String.IsNullOrEmpty(method) ? "" : "/" + method); 61 StartCoroutine(PostWWW(url, form, result)); 62 } 63 64 65 66 IEnumerator PostWWW(string url, WWWForm form, Action<WWW> result) 67 { 68 var www = new WWW(url, form); 69 yield return www; 70 if (string.IsNullOrEmpty(www.error)) 71 { 72 if (result != null) 73 result(www); 74 } 75 else 76 { 77 if (result != null) 78 result(www); 79 Debug.Log("post error+" + www.error.ToString() + "==url==" + url); 80 } 81 } 82 83 public void GetData(string _path, Action<UnityWebRequest> result) 84 { 85 string url = AddressDataUrl + _path; 86 StartCoroutine(GetWWWW(url, result)); 87 } 88 89 IEnumerator GetWWWW(string url, Action<UnityWebRequest> result) 90 { 91 //while (Application.internetReachability == NetworkReachability.NotReachable) 92 //{ 93 // yield return 0; 94 //} 95 /* 96 var www = new WWW(url); 97 yield return www; 98 if (result != null) 99 result(www); 100 if (!string.IsNullOrEmpty(www.error)) 101 Debug.Log("get error+" + www.error.ToString() + "==url==" + url);*/ 102 103 UnityWebRequest uwr = UnityWebRequest.Get(url); 104 yield return uwr.SendWebRequest(); 105 if (result!=null) 106 { 107 result(uwr); 108 } 109 if (uwr.isHttpError||uwr.isNetworkError) 110 { 111 Debug.Log(string.Format("Get error: {0}, url: {1}",uwr.error,url)); 112 } 113 } 114 115 public void GetDataByUrl(string _url, Action<UnityWebRequest> result)//直接通过url获取资源 116 { 117 StartCoroutine(GetWWWW(_url, result)); 118 } 119 120 public void Post(string url, Hashtable data, Action<WWW> result) 121 { 122 WWWForm form = new WWWForm(); 123 foreach (string key in data.Keys) 124 { 125 form.AddField(key, data[key].ToString()); 126 } 127 StartCoroutine(PostWWW(url, form, result)); 128 } 129 } 130 }