I have a Class that is failing when running in WebGL but it works in the UNITY IDE (5.6.1f1 Personal (Plus) Edition.) The code is 'trimmed' below, but yields the same characteristics (fails as a WebGL and runs w/ no issues in the UNITY IDE.) I'm pointing this to a service URL and get back a proper response when testing, but the Post actually never occurs when running from the WebGL and will crash when no response (not even an error) is received. I would like to get thoughts from the community (maybe I need to set a specific build parameter or modify the code implementation?) Helpful feedback would be much appreciated. Thank you.
-------------------- Wrapper and JSON Utility Classes ----------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JsonTest : MonoBehaviour {
JsonCommunicationManager jsonComm;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnMouseDown()
{
var jsonCommunications = gameObject.AddComponent<JsonCommunicationManager>();
string tempReturn = jsonCommunications.PostStartUpRequest("{\"userId\":1,\"id\":1}");
Debug.Log("JSON RequestStartParms: Response : " + tempReturn);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JsonCommunicationManager : MonoBehaviour
{
private WWW wwwForm;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public string PostStartUpRequest(string JsonPostMessage)
{
Debug.Log("Inside PostMessage:");
string JsonReturnMessage;
StartCoroutine(PostWWWMessage(JsonPostMessage));
bool boolResponse = false;
do
{
Debug.Log("Checking for Response ");
try
{
JsonReturnMessage = wwwForm.text;
boolResponse = true;
}
catch
{
WaitForResponse();
Debug.Log("Inside JsonPost Message: WAIT");
}
} while (!boolResponse);
JsonReturnMessage = wwwForm.text;
Debug.Log("Inside JsonPost Message: Messgae Response Received: ");
Debug.Log("Inside JsonPost Message: Messgae Response Data: " + JsonReturnMessage);
Debug.Log("Inside JsonPost Message: Messgae Response Received: ");
Debug.Log("Inside JsonPost Message: Messgae Response Error: " + wwwForm.error);
Debug.Log("Inside JsonPost Message: Messgae Response Received: ");
return JsonReturnMessage;
}
//private void PostWWWMessage(string JsonPostMessage) {
private IEnumerator PostWWWMessage(string JsonPostMessage)
{
Debug.Log("Inside PostWWWMessage:");
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Content-Type", "application/json");
byte[] postData = System.Text.Encoding.ASCII.GetBytes(JsonPostMessage.ToCharArray());
string fullyQualifiedURL = "https://jsonplaceholder.typicode.com/posts";
Debug.Log("Inside PostWWWMessage: Posting Message: " + JsonPostMessage);
print("Posting start up request to: " + fullyQualifiedURL);
print("Post Data is: " + postData);
print("Post Header is: " + headers);
wwwForm = new WWW(fullyQualifiedURL, postData, headers);
WaitForResponse();
yield return null;
}
private IEnumerator WaitForResponse()
{
yield return new WaitForSeconds(1);
}
}