C# WebGL中的统一WWW-Post失败
我有一个类在WebGL中运行时失败,但它在UNITY IDE(5.6.1f1个人(Plus)版)中工作。下面对代码进行了“修剪”,但产生了相同的特征(与WebGL一样失败,在UNITY IDE中运行时没有问题)。我将其指向一个服务URL,并在测试时得到正确的响应,但是,当从WebGL运行时,Post实际上从未发生,并且当没有收到响应(甚至没有错误)时,Post将崩溃。我想从社区中得到一些想法(也许我需要设置一个特定的构建参数或修改代码实现?)非常感谢有帮助的反馈。多谢各位 --------------包装器和JSON实用程序类--------------------C# WebGL中的统一WWW-Post失败,c#,json,unity3d,unity-webgl,C#,Json,Unity3d,Unity Webgl,我有一个类在WebGL中运行时失败,但它在UNITY IDE(5.6.1f1个人(Plus)版)中工作。下面对代码进行了“修剪”,但产生了相同的特征(与WebGL一样失败,在UNITY IDE中运行时没有问题)。我将其指向一个服务URL,并在测试时得到正确的响应,但是,当从WebGL运行时,Post实际上从未发生,并且当没有收到响应(甚至没有错误)时,Post将崩溃。我想从社区中得到一些想法(也许我需要设置一个特定的构建参数或修改代码实现?)非常感谢有帮助的反馈。多谢各位 -----------
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);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类JsonTest:MonoBehavior{
JsonCommunicationManager jsonComm;
//用于初始化
无效开始(){
}
//每帧调用一次更新
无效更新(){
}
公屋空置
{
var jsonCommunications=gameObject.AddComponent();
字符串tempreurn=jsonCommunications.PostStartUpRequest(“{\'userId\':1,\'id\':1}”);
Log(“JSON RequestStartParams:Response:”+tempReturn);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类JsonCommunicationManager:MonoBehavior
{
私人WWW-wwform;
//用于初始化
void Start()
{
}
//每帧调用一次更新
无效更新()
{
}
公共字符串PostStartUpRequest(字符串JsonPostMessage)
{
Log(“内部PostMessage:”);
字符串JsonReturnMessage;
start例程(postwwmessage(JsonPostMessage));
bool boolResponse=false;
做
{
Log(“检查响应”);
尝试
{
JsonReturnMessage=wwwForm.text;
boolResponse=true;
}
抓住
{
WaitForResponse();
Log(“内部JsonPost消息:WAIT”);
}
}而(!boolResponse);
JsonReturnMessage=wwwForm.text;
Log(“内部JsonPost消息:收到Messgae响应:”);
Log(“内部JsonPost消息:消息响应数据:“+JsonReturnMessage”);
Log(“内部JsonPost消息:收到Messgae响应:”);
Log(“内部JsonPost消息:Messgae响应错误:“+wwwForm.Error”);
Log(“内部JsonPost消息:收到Messgae响应:”);
返回JsonReturnMessage;
}
//私有void postwwmessage(字符串JsonPostMessage){
私有IEnumerator PostWWWMessage(字符串JsonPostMessage)
{
Log(“内部PostWWWMessage:”);
字典头=新字典();
添加(“内容类型”、“应用程序/json”);
byte[]postData=System.Text.Encoding.ASCII.GetBytes(JsonPostMessage.ToCharArray());
字符串fullyQualifiedURL=”https://jsonplaceholder.typicode.com/posts";
Log(“内部PostWWWMessage:Posting消息:“+JsonPostMessage”);
打印(“将启动请求过账至:+fullyQualifiedURL”);
打印(“Post数据为:”+postData);
打印(“帖子标题为:”+标题);
wwwForm=新的WWW(完全合格的文件、postData、标题);
WaitForResponse();
收益返回空;
}
私有IEnumerator WaitForResponse()
{
返回新的WaitForSeconds(1);
}
}
这就是当你试图在不了解协同程序的情况下构建完整程序时会发生的情况。我鼓励你创建一个新项目,找到一个协同程序教程并研究它是如何工作的。我认为这是理解协同程序的最佳方式
代码中存在错误:
1。正在尝试使用协程在void
函数中等待。在这里调用WaitForResponse()
在PostStartUpRequest
函数内。这将不等待,因为PostStartUpRequest
是一个无效函数。生成PostStartUpRequest
一个IEnumerator
函数,然后使用返回WaitForResponse();
等待
2。在void
函数中有一个while
循环,等待另一个函数中的另一个变量状态改变。while(!boolResponse);
不是一个好主意,除非你是从另一个线程执行,但你不是。这将冻结Unity,因为你没有给其他函数运行的机会。你可以通过在while循环中添加yield return null;
来解决这个问题,while循环在每次检查后都会等待帧。这允许其他函数运行。你必须更改>为此,将PostStartUpRequest
函数发送到IEnumerator
函数
do
{
//Wait for a frame
yield return null;
....
} while (!boolResponse);
你可能会遇到的车祸来自这里
3。当您从postwwmessage
函数调用WaitForResponse();
函数等待1秒时,这不应该起作用,因为您没有屈服。您必须等待WaitForResponse()
函数完成等待。您可以通过屈服来实现这一点
这应该是yield return WaitForResponse();
而不是WaitForResponse()
4。未正确等待WWW
请求完成。Webrequest取决于设备和互联网的速度。有时,它可能需要超过您等待的秒数。您必须生成WWW
请求,而不是等待1秒
//Make request
wwwForm = new WWW(fullyQualifiedURL, postData, headers);
//Wait for request to finish
yield return wwwForm;
//Now you can safely use it:
JsonReturnMessage = wwwForm.text;
5。在访问web请求结果之前不检查错误
在访问结果之前,您需要检查是否存在可能的错误,否则,可能会发生类似的情况
if (String.IsNullOrEmpty(wwwForm.error))
{
//No Error. Access result
JsonReturnMessage = wwwForm.text;
}else{
//Error while making a request
Debug.Log(wwwForm.error);
}
private WWW wwwForm;
// Use this for initialization
void Start()
{
StartCoroutine(PostWWWMessage("Test"));
}
//private void PostWWWMessage(string JsonPostMessage) {
public 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);
//Wait for the request
yield return wwwForm;
string JsonReturnMessage;
//Check for error
if (String.IsNullOrEmpty(wwwForm.error))
{
//No Error. Access result
JsonReturnMessage = wwwForm.text;
Debug.Log("Received: " + JsonReturnMessage);
}
else
{
//Error while making a request
Debug.Log(wwwForm.error);
}
}