diff --git a/source/Assets/Scripts/InfinarioSDK/FakeObject.cs b/source/Assets/Scripts/InfinarioSDK/FakeObject.cs new file mode 100644 index 0000000..527499b --- /dev/null +++ b/source/Assets/Scripts/InfinarioSDK/FakeObject.cs @@ -0,0 +1,6 @@ +using UnityEngine; + +namespace Infinario.Sender { + public class FakeObject : MonoBehaviour{ + } +} diff --git a/source/Assets/Scripts/InfinarioSDK/Sender.cs b/source/Assets/Scripts/InfinarioSDK/Sender.cs index da26433..ad438bb 100644 --- a/source/Assets/Scripts/InfinarioSDK/Sender.cs +++ b/source/Assets/Scripts/InfinarioSDK/Sender.cs @@ -4,7 +4,9 @@ using Infinario.Storage; using Infinario.MiniJSON; using System.Text; - +#if UNITY_5_4_OR_NEWER +using UnityEngine.Networking; +#endif namespace Infinario.Sender { class Sender @@ -22,7 +24,7 @@ protected void StartCoroutine(IEnumerator coroutine) { var go = new GameObject("Infinario Coroutines"); UnityEngine.Object.DontDestroyOnLoad(go); - _coroutineObject = go.AddComponent(); + _coroutineObject = go.AddComponent(); } _coroutineObject.StartCoroutine (coroutine); } @@ -54,21 +56,36 @@ private IEnumerator APISendLoop(string target, PersistentBulkCommandQueue comman var httpBody = Json.Serialize(new Dictionary {{"commands", commands}}); byte[] httpBodyBytes = Encoding.UTF8.GetBytes(httpBody); Dictionary httpHeaders = new Dictionary{ {"Content-type", "application/json"} }; - - // 2. Send the bulk API request - WWW req = new WWW(httpTarget, httpBodyBytes, httpHeaders); //TODO: we could add a timeout functionality - yield return req; - - // 3A: Check response for errors - if (!string.IsNullOrEmpty(req.error)) + + // 2. Send the bulk API request +#if UNITY_5_4_OR_NEWER + UnityWebRequest req = new UnityWebRequest(httpTarget, "POST"); //TODO: we could add a timeout functionality + + foreach (var header in httpHeaders) + req.SetRequestHeader(header.Key, header.Value); + + req.uploadHandler = new UploadHandlerRaw(httpBodyBytes); + req.downloadHandler = new DownloadHandlerBuffer(); + + yield return req.Send(); +#else + WWW req = new WWW(httpTarget, httpBodyBytes, httpHeaders); //TODO: we could add a timeout functionality + yield return req; +#endif + // 3A: Check response for errors + if (!string.IsNullOrEmpty(req.error)) { consecutiveFailedRequests++; } else { - // 3B. Parse the API response - var responseBody = req.text; - Dictionary apiResponse = (Dictionary) Json.Deserialize(responseBody); + // 3B. Parse the API response +#if UNITY_5_4_OR_NEWER + var responseBody = req.downloadHandler.text; +#else + var responseBody = req.text; +#endif + Dictionary apiResponse = (Dictionary) Json.Deserialize(responseBody); bool success = (bool) apiResponse["success"]; if(success) {