Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions source/Assets/Scripts/InfinarioSDK/FakeObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using UnityEngine;

namespace Infinario.Sender {
public class FakeObject : MonoBehaviour{
}
}
41 changes: 29 additions & 12 deletions source/Assets/Scripts/InfinarioSDK/Sender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +24,7 @@ protected void StartCoroutine(IEnumerator coroutine)
{
var go = new GameObject("Infinario Coroutines");
UnityEngine.Object.DontDestroyOnLoad(go);
_coroutineObject = go.AddComponent<MonoBehaviour>();
_coroutineObject = go.AddComponent<FakeObject>();
}
_coroutineObject.StartCoroutine (coroutine);
}
Expand Down Expand Up @@ -54,21 +56,36 @@ private IEnumerator APISendLoop(string target, PersistentBulkCommandQueue comman
var httpBody = Json.Serialize(new Dictionary<string,object> {{"commands", commands}});
byte[] httpBodyBytes = Encoding.UTF8.GetBytes(httpBody);
Dictionary<string,string> httpHeaders = new Dictionary<string,string>{ {"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<string, object> apiResponse = (Dictionary<string, object>) 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<string, object> apiResponse = (Dictionary<string, object>) Json.Deserialize(responseBody);
bool success = (bool) apiResponse["success"];
if(success)
{
Expand Down