private int sendGetHtml(string _strUrl, string _strJson) { m_eConnect = EConnect.CONNECTING; // 通信に利用するシリアル番号を発行する m_iNetworkSerial += 1; StartCoroutine(getHtml(_strUrl, _strJson, m_iNetworkSerial)); return(m_iNetworkSerial); }
//================================================================================== // ==================================================================================== // 以下シングルトン宣言 private void initialize() { m_eConnect = EConnect.DISCONNECT; m_eStatus = EStatus.SLEEP; m_bErrorLog = false; SESSION_ERROR = false; m_intTimeoutTime = TIMEOUT_TIME; IsMaintenance = false; m_dictNetworkData.Clear(); return; }
private IEnumerator ResponseConnectJson(WWW www, float timeout) { float requestTime = Time.time; while (!www.isDone) { if (Time.time - requestTime < timeout) { yield return(null); } else { //Debug.LogWarning("TimeOut"); //タイムアウト m_eConnect = EConnect.ERROR_TIMEOUT; // エラー状態にする break; } } yield return(www); }
private int sendNetwork(string _strUrl, string _strJson) { m_eConnect = EConnect.CONNECTING; // 通信に利用するシリアル番号を発行する m_iNetworkSerial += 1; StartCoroutine (sendModule (_strUrl, _strJson, m_iNetworkSerial)); return m_iNetworkSerial; }
IEnumerator sendModule(string _strUrl, string _strJson, int _iNetworkSerial) { // 通信用に収納 WWWForm form = new WWWForm (); form.AddField ("req_data", _strJson); Debug.Log (_strUrl + _strJson); string strOutput = ""; WWW www = new WWW (_strUrl, form); //何も返ってこない場合 // リクエストを受け取る処理(別メソッドでタイムアウト判定) yield return StartCoroutine (ResponseConnectJson (www, (float)m_intTimeoutTime)); // リクエストエラーの場合 if (!string.IsNullOrEmpty (www.error)) { m_intErrorCode = UNKNOWN_ERROR_CODE; m_eConnect = EConnect.ERROR_UNKNOWN; Debug.Log (string.Format ("Fail Whale!\n{0}", www.error)); yield break; // コルーチンを終了 //タイムアウトエラーだった場合 } else if (m_eConnect == EConnect.ERROR_TIMEOUT) { if (m_bErrorLog) { Debug.Log ("timeout_error"); } yield break; // コルーチンを終了 //リクエストしたのに空で返ってきた場合 } else if (string.IsNullOrEmpty (www.text)) { if (m_bErrorLog) { Debug.Log ("no items"); } yield break; // コルーチンを終了 } else { SafeDebutLog ("success:" + www.text); string decodedText = ""; strOutput = www.text; } IDictionary dictRecieveData; #if USE_NETWORK_MINIJSON dictRecieveData = (IDictionary)Json.Deserialize(strOutput); #else dictRecieveData = null; #endif m_dictRecievedData = dictRecieveData; // これは直接利用しない // 受信したデータを格納する TNetworkData tNetworkData = new TNetworkData (_iNetworkSerial); tNetworkData.m_iSerial = _iNetworkSerial; tNetworkData.m_dictRecievedData = dictRecieveData; tNetworkData.m_strData = strOutput; tNetworkData.m_bIsError = false; tNetworkData.m_iErrorCode = 0; bool bIsError = false; bool bErrorCode = false; if (dictRecieveData != null) { bErrorCode = dictRecieveData.Contains (JSON_ERROR_CODE); } int iErrorCode = 0; if (bErrorCode) { iErrorCode = (int)(long)dictRecieveData [JSON_ERROR_CODE]; if (0 < iErrorCode) { bIsError = true; tNetworkData.m_bIsError = true; tNetworkData.m_iErrorCode = iErrorCode; } } m_intErrorCode = 0; EConnect eConnect = EConnect.CONNECTING; if (bIsError) { eConnect = EConnect.ERROR_RECIEVED; //Debug.Log( "m_intErrorCode=" + m_intErrorCode ); if (m_intErrorCode == MAINTENANCE_ERROR_CODE) { // 状態は変更しません IsMaintenance = true; } else if (m_intErrorCode == SESSION_ERROR_CODE) { IsSessionError = true; } else { eConnect = EConnect.ERROR_RECIEVED; } } else { eConnect = EConnect.CONNECTED; } m_eConnect = eConnect; tNetworkData.m_eConnect = eConnect; m_dictNetworkData.Remove (_iNetworkSerial); m_dictNetworkData.Add (_iNetworkSerial, tNetworkData); yield break; // コルーチン終了 }
private IEnumerator ResponseConnectJson(WWW www, float timeout) { float requestTime = Time.time; while (!www.isDone) { if (Time.time - requestTime < timeout) { yield return null; } else { //Debug.LogWarning("TimeOut"); //タイムアウト m_eConnect = EConnect.ERROR_TIMEOUT; // エラー状態にする break; } } yield return www; }
//================================================================================== // ==================================================================================== // 以下シングルトン宣言 private void initialize() { m_eConnect = EConnect.DISCONNECT; m_eStatus = EStatus.SLEEP; m_bErrorLog = false; SESSION_ERROR = false; m_intTimeoutTime = TIMEOUT_TIME; IsMaintenance = false; m_dictNetworkData.Clear (); return; }
// Events protected virtual void OnConnect() { EConnect?.Invoke(this, EventArgs.Empty); }
// 共通部おいのでまとめれそう IEnumerator getHtml(string _strUrl, string _strJson, int _iNetworkSerial) { // 通信用に収納 WWWForm form = new WWWForm(); form.AddField("req_data", _strJson); string url = _strUrl; // Debug.Log(url + _strJson); SafeDebutLog(url + _strJson); string strOutput = ""; WWW www = new WWW(url, form); //何も返ってこない場合 // リクエストを受け取る処理(別メソッドでタイムアウト判定) yield return(StartCoroutine(ResponseConnectJson(www, (float)m_intTimeoutTime))); // リクエストエラーの場合 if (!string.IsNullOrEmpty(www.error)) { m_intErrorCode = UNKNOWN_ERROR_CODE; m_eConnect = EConnect.ERROR_UNKNOWN; Debug.Log(string.Format("Fail Whale!\n{0}", www.error)); yield break; // コルーチンを終了 //タイムアウトエラーだった場合 } else if (m_eConnect == EConnect.ERROR_TIMEOUT) { if (m_bErrorLog) { Debug.Log("timeout_error"); } yield break; // コルーチンを終了 //リクエストしたのに空で返ってきた場合 } else if (string.IsNullOrEmpty(www.text)) { if (m_bErrorLog) { Debug.Log("no items"); } yield break; // コルーチンを終了 } else { SafeDebutLog("success:" + www.text); string decodedText = ""; strOutput = www.text; } IDictionary dictRecieveData; #if USE_NETWORK_MINIJSON dictRecieveData = (IDictionary)Json.Deserialize(strOutput); #else dictRecieveData = null; #endif m_dictRecievedData = dictRecieveData; // これは直接利用しない // 受信したデータを格納する TNetworkData tNetworkData = new TNetworkData(_iNetworkSerial); tNetworkData.m_iSerial = _iNetworkSerial; tNetworkData.m_dictRecievedData = dictRecieveData; tNetworkData.m_strData = strOutput; tNetworkData.m_bIsError = false; tNetworkData.m_iErrorCode = 0; bool bIsError = false; bool bErrorCode = false; if (dictRecieveData != null) { bErrorCode = dictRecieveData.Contains(JSON_ERROR_CODE); } int iErrorCode = 0; if (bErrorCode) { iErrorCode = (int)(long)dictRecieveData [JSON_ERROR_CODE]; if (0 < iErrorCode) { bIsError = true; tNetworkData.m_bIsError = true; tNetworkData.m_iErrorCode = iErrorCode; } } m_intErrorCode = 0; EConnect eConnect = EConnect.CONNECTING; if (bIsError) { eConnect = EConnect.ERROR_RECIEVED; //Debug.Log( "m_intErrorCode=" + m_intErrorCode ); if (m_intErrorCode == MAINTENANCE_ERROR_CODE) { // 状態は変更しません IsMaintenance = true; } else if (m_intErrorCode == SESSION_ERROR_CODE) { IsSessionError = true; } else { eConnect = EConnect.ERROR_RECIEVED; } } else { eConnect = EConnect.CONNECTED; } m_eConnect = eConnect; tNetworkData.m_eConnect = eConnect; m_dictNetworkData.Remove(_iNetworkSerial); m_dictNetworkData.Add(_iNetworkSerial, tNetworkData); yield break; }