/// <summary> /// 帐号,密码登陆app /// </summary> /// <param name="userName"></param> /// <param name="pwd"></param> /// <param name="device"></param> /// <param name="platform"></param> /// <param name="loginActionCallBack"></param> /// <returns></returns> public bool LoginToApp(string userName, string pwd, Action <int> loginActionCallBack = null) { _loginActionCallBack = loginActionCallBack; if (string.IsNullOrEmpty(userName)) { PromptManager.Instance.MessageBox(PromptManager.Type.FloatingTip, "用户名不能为空"); _loginActionCallBack.Invoke(2); return(false); } if (string.IsNullOrEmpty(pwd)) { PromptManager.Instance.MessageBox(PromptManager.Type.FloatingTip, "密码不能为空"); _loginActionCallBack.Invoke(2); return(false); } //btn_login.enabled = false; _userName = userName; _pwd = pwd; PlayerPrefs.SetString("user_cache_name", _userName); PlayerPrefs.SetString("user_cache_pwd", _pwd); JsonData rpcObj = new JsonData(); rpcObj["clientDate"] = SnapHttpManager.GetTimeStamp(); rpcObj["username"] = userName; rpcObj["password"] = pwd; rpcObj["version"] = MiscUtils.GetVersion(); rpcObj["device"] = SystemInfo.deviceType.ToString(); rpcObj["platform"] = SystemInfo.operatingSystem; rpcObj["deviceNew"] = SystemInfo.deviceType.ToString(); rpcObj["gameDeviceID"] = SelfPlayerData.DeviceID; SnapAppApi.Request_SnapAppApi(SnapAppApiInterface.Request_LoginApp, SnapHttpConfig.NET_REQUEST_POST, rpcObj, onLoginRequestFinish); return(true); }
public static SnapHttpManager getInstance() { if (instance == null) { instance = new SnapHttpManager(); } return(instance); }
private void CheckResponseError(SnapRpcDataVO rpcData) { if (!rpcData.status) { LogManager.LogError("[HttpRequest]Check Response Error! error code: " , rpcData.code, ", err message: ", rpcData.message); if (rpcData.code == (int)HttpErrorCode.LoginTokenError) { GameManager.LoginOnOtherDivices(false); } } SnapHttpManager.getInstance().AddOneHttpCallBack(httpRequestVO.requestBackAction, rpcData); }
public static HttpWebRequest PutRequest(SnapRequestVO vo, Action <SnapRpcDataVO> callback = null) { string request_url = GetHttpURL(vo); if (string.IsNullOrEmpty(request_url)) { request_url = SnapHttpConfig.NET_APP_URL; } byte[] body = vo.toByte(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(request_url); request.Proxy = null; //LogManager.Log(request.RequestUri); request.Method = SnapHttpConfig.NET_REQUEST_PUT; request.Timeout = 600 * 1000; request.ReadWriteTimeout = 600 * 1000; request.ContentType = SnapAppUploadFile.OSS_ContentType; request.ContentLength = body.Length; if (vo.mp3byte != null && vo.mp3byte.Length > 0) { MethodInfo priMethod = request.Headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic); priMethod.Invoke(request.Headers, new[] { "Date", SnapAppUploadFile.OSS_Date }); priMethod.Invoke(request.Headers, new[] { "Content-Length", body.Length.ToString() }); priMethod.Invoke(request.Headers, new[] { "Content-Type", SnapAppUploadFile.OSS_ContentType }); request.Headers.Add("Authorization", SnapAppUploadFile.OSS_Authorization); } else { request.Headers.Add("Token", SnapHttpConfig.NOT_LOGINED_APP_TOKEN); request.Headers.Add("UserToken", SnapHttpConfig.LOGINED_APP_TOKEN); } try { if (body != null && body.Length > 0) { using (Stream stream = request.GetRequestStream()) { stream.Write(body, 0, body.Length); stream.Flush(); stream.Close(); } } if (callback != null) { //音频文件 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode == HttpStatusCode.OK) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { SnapRpcDataVO rpcData = new SnapRpcDataVO(); rpcData.code = 1; rpcData.message = "request success"; rpcData.data = new JsonData(); rpcData.data["uploadUrl"] = response.ResponseUri.AbsolutePath; //if (callback != null) //{ // callback.Invoke(rpcData); // callback = null; //} SnapHttpManager.getInstance().AddOneHttpCallBack(callback, rpcData); reader.Close(); } } response.Close(); request.Abort(); } } } catch (Exception ex) { LogManager.Log(ex.Message); throw; } return(request); }
public static HttpErrorCodeDelegate RequestErrorCodeBack = null; //登录回调 /// <summary> /// 请求App新版本的api /// </summary> /// <param name="requestInterType">请求接口名称,枚举值</param> /// <param name="requestMethod">请求接口方法SnapHttpConfig.NET_REQUEST_GET/SnapHttpConfig.NET_REQUEST_POST</param> /// <param name="jsonData">发送请求的数据</param> /// <param name="callBackAction">服务器返回数据的callback事件</param> /// <param name="priority">发送请求的优先级</param> static public void Request_SnapAppApi(SnapAppApiInterface requestInterType, string requestMethod, JsonData jsonData = null, Action <SnapRpcDataVO> callBackAction = null, int priority = 0, SnapAppApiThirdRequestFace third = SnapAppApiThirdRequestFace.No_Third) { string apiName = ""; switch (requestInterType) { case SnapAppApiInterface.Request_LoginApp: apiName = User_Service_Login; break; case SnapAppApiInterface.Request_GetUserInfo: apiName = User_Service_Get_Info; break; case SnapAppApiInterface.Request_RegisterApp: apiName = User_Service_Register; break; case SnapAppApiInterface.Request_RegisterGuestApp: apiName = Guest_Service_Register; break; case SnapAppApiInterface.Request_CheckUserNameApp: apiName = User_Service_CheckUserName; break; case SnapAppApiInterface.Request_ModifyPwdApp: apiName = User_Service_ModifyPwd; break; case SnapAppApiInterface.Request_AliyunOSSApp: apiName = Request_Aliyun_OSS; break; case SnapAppApiInterface.Request_ThirdWebToApp: //第三方的都走这个,需要什么数据自己封装 apiName = jsonData.TryGetString("apiName"); break; case SnapAppApiInterface.Request_WeChatShareGame: apiName = User_Service_WeChatShare; break; case SnapAppApiInterface.Request_UploadGameInfo: apiName = User_Service_UploadGame_Info + "/" + jsonData.TryGetString("uid"); break; case SnapAppApiInterface.Request_UpdateUserInfo: apiName = User_Service_UpdateUserInfo; break; case SnapAppApiInterface.Request_GetGuestUserInfoByDeviceId: apiName = Guest_Service_GetInfoByDeviceId; break; case SnapAppApiInterface.Request_GetLevelScore: apiName = User_Service_GetLevelScore; break; case SnapAppApiInterface.Request_GetRankScoresByLevelID: apiName = User_Service_GetRankScores + "/" + jsonData.TryGetString("levelID"); break; case SnapAppApiInterface.Request_GetRankOfTenByIDs: apiName = User_Service_GetRankOfTenByID; break; case SnapAppApiInterface.Request_UploadGameScores: apiName = User_Service_UploadGameScores + "/"; break; case SnapAppApiInterface.Request_IAPGetGoodsInfo: apiName = User_IAP_GetGoodsInfo; break; case SnapAppApiInterface.Request_IAPGetGoodsVerify: apiName = User_IAP_Verify; break; case SnapAppApiInterface.Request_CurAppVersion: apiName = App_GetCurrentVersion; break; case SnapAppApiInterface.Request_UploadLevelVoices: apiName = User_Service_UploadLevelVoices; break; case SnapAppApiInterface.Request_GetLevelVoices: apiName = User_Service_UploadLevelVoices; break; } //SnapHttpConfig.REQUEST_APP_API_TOKEN = (apiName.IndexOf (User_Anonymous_Header) != -1) ? //SnapHttpConfig.NOT_LOGINED_APP_TOKEN : SnapHttpConfig.LOGINED_APP_TOKEN; SnapHttpManager.getInstance().Request_SnapAppApi(requestInterType, apiName, requestMethod, jsonData, callBackAction, priority, third); }
private void Update() { SnapHttpManager.getInstance().update(); }
/// <summary> /// 获取http请求返回的数据 /// </summary> private void GetHttpResponse(IAsyncResult result) { HttpWebRequest req = (HttpWebRequest)result.AsyncState; using (response = (HttpWebResponse)req.GetResponse()) { GetServerTime(response.Headers); if (response.StatusCode == HttpStatusCode.OK) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { JsonData jsonData = JsonMapper.ToObject(new JsonReader(reader.ReadToEnd())); SnapRpcDataVO rpcData = new SnapRpcDataVO(); if (jsonData != null) { rpcData.action = httpRequestVO.requestAction; rpcData.data = jsonData["data"]; rpcData.status = (bool)jsonData["status"]; if (rpcData.status == true) { rpcData.code = 1; } else { rpcData.code = int.Parse(jsonData.TryGetString("code")); } rpcData.message = jsonData["message"].ToString(); } close(); LogManager.LogWarning(" - Result:", JsonMapper.ToJson(jsonData)); CheckResponseError(rpcData); //if (httpRequestVO.requestBackAction != null) //{ // httpRequestVO.requestBackAction.Invoke(rpcData); // httpRequestVO.requestBackAction = null; //} } } else { LogManager.Log(response.StatusCode); close(); if (response.StatusCode == HttpStatusCode.RequestTimeout || response.StatusCode == HttpStatusCode.GatewayTimeout) { //需要重新请求的错误异常 if (needReload) { LogManager.Log("reconnect"); if (httpRequestVO.requestErrCount < SnapHttpConfig.NET_RETRY_NUM) { httpRequestVO.requestErrCount++; SnapHttpManager.getInstance().Re_Request_SnapAppApi_Inner(httpRequestVO); } } } else { //全局异常 if (SnapAppApi.RequestErrorCodeBack != null) { SnapAppApi.RequestErrorCodeBack.Invoke((int)response.StatusCode); } } } } }
/// <summary> /// 把本地文件上传到oss,默认为根据头文件进行上传 /// </summary> /// <param name="filePath"></param> /// <param name="uploadType"></param> /// <param name="resultAction"></param> public void PutObjectFileToOSS(string filePath, Action <string> resultAction = null) { _resultAction = resultAction; string[] arr = filePath.Split('.'); string[] arr2 = arr[0].ToString().Split('/'); string fileExtType = arr[1]; //game / upload /{ YYYYMMdd}/{ user_uuid}/{ timestamp}.mp3 int index = 0; if (DebugConfigController.Instance.FormalData) { // 正式服 index = 1; } UploadOSSPathHeader = ConfigurationController.Instance.OSSListGamePaths[index] + "/"; UploadOSSPathHeader += DateTime.UtcNow.ToString("yyyyMMdd") + "/" + SelfPlayerData.Uuid + "/" + SnapHttpManager.GetTimeStamp(); if (fileExtType == "txt" || fileExtType == "json" || fileExtType == "xml") { _filePath = filePath; fileUploadKey = UploadOSSPathHeader + "." + arr[1]; _fileExtType = 0; GetFileKey(() => { PutObjectWithHeader(_resultAction); }); } else if (fileExtType == "mp3" || fileExtType == "wav") { fileUploadKey = UploadOSSPathHeader + ".mp3"; _fileExtType = 1; //wav to mp3 string mp3FileName = arr2[arr2.Length - 1] + ".mp3"; string mp3FilePath = ""; for (int i = 0; i < arr2.Length - 1; i++) { mp3FilePath += arr2[i] + "/"; } mp3FilePath = mp3FilePath.Substring(0, mp3FilePath.Length - 1); _filePath = Path.Combine(mp3FilePath, mp3FileName); WaveToMP3(filePath, _filePath, LAMEPreset.ABR_128); } }