Exemplo n.º 1
0
    private void GetDataAsync(string url, string id, OnDataCompleted handler, GameObject gameObject)
    {
#if UNITY_WSA && !UNITY_EDITOR
        IAsyncAction asyncAction = Windows.System.Threading.ThreadPool.RunAsync(
            async(workItem) =>
        {
            try
            {
                WebRequest webRequest = WebRequest.Create(url);
                webRequest.Method     = "GET";
                webRequest.Headers["Content-Type"] = "application/json";

                WebResponse response = await webRequest.GetResponseAsync();

                Stream result       = response.GetResponseStream();
                StreamReader reader = new StreamReader(result);

                string json = reader.ReadToEnd();

                handler(id, DateTime.Now, json, gameObject);
            }
            catch (Exception)
            {
                // handle errors
            }
        }
            );
        asyncAction.Completed = new AsyncActionCompletedHandler(GetDataAsyncCompleted);
#endif
    }
Exemplo n.º 2
0
    private void PostDataAsync(Uri uri, string jsonRequestBody, string id, OnDataCompleted handler, GameObject gameObject)
    {
        string url       = uri.AbsoluteUri;
        var    jsonBytes = System.Text.Encoding.UTF8.GetBytes(jsonRequestBody);

#if UNITY_WSA && !UNITY_EDITOR
        IAsyncAction asyncAction = Windows.System.Threading.ThreadPool.RunAsync(
            async(workItem) =>
        {
            WebRequest webRequest = WebRequest.Create(url);
            webRequest.Method     = "POST";
            webRequest.Headers["Content-Type"] = "application/json";

            Stream stream = await webRequest.GetRequestStreamAsync();
            stream.Write(jsonBytes, 0, jsonBytes.Length);
            //TODO : get response and update GameState
            WebResponse response = await webRequest.GetResponseAsync();
            Stream result        = response.GetResponseStream();
            StreamReader reader  = new StreamReader(result);

            string json = reader.ReadToEnd();

            handler(id, DateTime.Now, json, gameObject);
        }
            );

        asyncAction.Completed = new AsyncActionCompletedHandler(PostDataAsyncCompleted);
#endif
    }