본문 바로가기

서버&클라이언트

WEBRequest로 람다이벤트 실행

일단 다음과 같은 호출이 있다고 치자

  SystemManager.instance.StartCoroutine(CallbackData.Upload(url, JsonUtility.ToJson(this), 
            (object a)=> {
                getPaymentCallback getDB = new getPaymentCallback();
                JsonUtility.FromJsonOverwrite((string)a, getDB);
                if (getDB.header.resCode != 1)
                    Debug.Log("error : " + a);
                else
                    PaymentManager.instance.pc = true;
            }));

 

Upload에서 리퀘스트 되었을 때에 람다 실행되니 대기탄다고 와일문 돌 필요가 없당.

public static class CallbackData
{
    public static IEnumerator Upload(string URL, string json, Action<object> at = null)
    {
        using (UnityWebRequest request = UnityWebRequest.Post(URL, json))
        {
            byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
            request.uploadHandler = new UploadHandlerRaw(jsonToSend);
            request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
            request.SetRequestHeader("Content-Type", "application/json");

            yield return request.SendWebRequest();

            if (request.isNetworkError || request.isHttpError)
                Debug.Log(request.error);
            else
            {
                if (at != null) 
                    at.Invoke(request.downloadHandler.text);
            }
        }
    }

'서버&클라이언트' 카테고리의 다른 글

서버의 기초.  (0) 2015.12.11