/// <summary> /// 登录,其他所有操作都要在登录成功后进行 /// 跟其他http谓词都不一样,没有排队,直接发送了 /// </summary> public void Login(string name, string password, OnHttpResponse cb) { var queryString = string.Format("email={0}&password={1}", name, password); var fullURL = LoginAddress + "?" + queryString; var seemsUselessBytes = Encoding.UTF8.GetBytes(queryString); var req = new WWWRequest() { Data = seemsUselessBytes, Headers = null, RespCallback = www => { if (www == null) { cb(GenTimeoutResp()); return; } var resp = new HttpResp(); if (!string.IsNullOrEmpty(www.error)) { resp.Error = HttpResp.ErrorType.NetworkError; resp.ErrorText = www.error; } else { var loginResp = JsonUtility.FromJson <LoginResp>(www.text); if (!string.IsNullOrEmpty(loginResp.error)) { resp.Error = HttpResp.ErrorType.LogicError; resp.ErrorText = loginResp.error; } else { resp.Error = HttpResp.ErrorType.None; //set global identity userId = loginResp.user_id; accessToken = loginResp.access_token; } } resp.WwwText = www.text; cb(resp); }, Type = RequestType.Login, Url = fullURL }; waitingRequests.Enqueue(req); }
//需要解json private HttpResp GenerateResp(WWW www) { if (www == null) { return(GenTimeoutResp()); } var resp = new HttpResp(); if (!string.IsNullOrEmpty(www.error)) { resp.Error = HttpResp.ErrorType.NetworkError; resp.ErrorText = www.error; } else { try { var respJson = JsonUtility.FromJson <JsonBase>(www.text); if (respJson.error == EXPIRED_ERROR) { resp.Error = HttpResp.ErrorType.AccessExpired; resp.ErrorText = resp.Error.ToString(); } else if (!string.IsNullOrEmpty(respJson.error)) { resp.Error = HttpResp.ErrorType.LogicError; resp.ErrorText = respJson.error; } else { resp.Error = HttpResp.ErrorType.None; } } catch (Exception e) { InfoTips.LogWarning("parse json error: " + e.ToString()); resp.Error = HttpResp.ErrorType.JsonError; resp.ErrorText = e.ToString(); } } resp.WwwText = www.text; return(resp); }
//不需要解json private HttpResp GenerateGetfileResp(WWW www) { if (www == null) { return(GenTimeoutResp()); } var resp = new HttpResp(); if (!string.IsNullOrEmpty(www.error)) { resp.Error = HttpResp.ErrorType.NetworkError; resp.ErrorText = www.error; } else { //allright resp.Error = HttpResp.ErrorType.None; resp.WwwText = www.text; } return(resp); }
/// <summary> /// 带本地缓存的http文件获取 /// 本地版本实际上是同步,网络版本是异步</summary> /// <param name="url"></param> /// <param name="type"></param> /// <param name="callback"></param> /// <param name="fromQY">是否是从青云服务器上下载,区别于:百度API获取街景</param> public void GetFile(string url, LocalCacheEntry.CacheType type, OnHttpGetFileResponse callback, bool fromQY = true) { if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("Attempt to getfile from an empty URL"); } // 文件缓存之后的文件名 var local = URLToLocalPath(url); InfoTips.LogInfo("[GetFile] related local path:" + local); // 缓存中已经存在 if (File.Exists(local)) { InfoTips.LogInfo("[GetFile] load from local"); var fakeResp = new HttpResp(); fakeResp.Error = HttpResp.ErrorType.None; fakeResp.WwwText = ""; switch (type) { case LocalCacheEntry.CacheType.Texture: var tex = new Texture2D(2, 2); var imgBytes = File.ReadAllBytes(local); tex.LoadImage(imgBytes); callback(fakeResp, new LocalCacheEntry() { Type = LocalCacheEntry.CacheType.Texture, Texture = tex }); break; case LocalCacheEntry.CacheType.Fbx: callback(fakeResp, new LocalCacheEntry() { Type = LocalCacheEntry.CacheType.Fbx, FbxPath = local }); break; case LocalCacheEntry.CacheType.AssetBundle: var ab = AssetBundle.LoadFromFile(local); callback(fakeResp, new LocalCacheEntry() { Type = LocalCacheEntry.CacheType.AssetBundle, AB = ab }); break; case LocalCacheEntry.CacheType.Raw: var bytes = File.ReadAllBytes(local); callback(fakeResp, new LocalCacheEntry() { Type = LocalCacheEntry.CacheType.Raw, Bytes = bytes }); break; default: break; } } else { InfoTips.LogInfo("[GetFile] load from remote"); string realUrl; NameValueCollection query; if (fromQY) { realUrl = GetHttpPrefix() + "entities/download"; query = new NameValueCollection() { { "path", url } }; } else { realUrl = url; query = null; } GetRaw(realUrl, query, www => { var resp = GenerateGetfileResp(www); if (resp.Error != HttpResp.ErrorType.None) { callback(resp, null); } else { //write cache //TODO: more scientific way: async var localPath = URLToLocalPath(url); if (localPath.Length > 250) { throw new IOException("localpath length overflowwww!!!"); } File.WriteAllBytes(localPath, www.bytes); switch (type) { case LocalCacheEntry.CacheType.Texture: callback(resp, new LocalCacheEntry() { Type = LocalCacheEntry.CacheType.Texture, Texture = www.texture }); break; case LocalCacheEntry.CacheType.Fbx: callback(resp, new LocalCacheEntry() { Type = LocalCacheEntry.CacheType.Fbx, FbxPath = localPath }); break; case LocalCacheEntry.CacheType.AssetBundle: callback(resp, new LocalCacheEntry() { Type = LocalCacheEntry.CacheType.AssetBundle, AB = www.assetBundle }); break; case LocalCacheEntry.CacheType.Raw: callback(resp, new LocalCacheEntry() { Type = LocalCacheEntry.CacheType.Raw, Bytes = www.bytes }); break; default: break; } } }); } }