Unity Webrequests执行顺序

时间:2019-01-09 15:42:05

标签: c# http unity3d

我有一个与Web服务器通信的客户端应用程序。

它正在发送多个不同的Web请求。

第一个用于在“较长”的时间间隔内提取消息:

 private IEnumerator UpdateRequest()
 {
     HasOpenUpdateRequest = true;

     WWWForm formData = new WWWForm();

     formData.AddField("action", ServerAction.Subscribe);
     formData.AddField("sid", sessionID);        

     using (UnityWebRequest request = UnityWebRequest.Post("removed_url", formData))
     {

         request.timeout = 30;
         Debug.Log(Time.time);
         UnityWebRequestAsyncOperation asyncOperation = request.SendWebRequest();
         yield return new WaitUntil(() => asyncOperation.isDone);
         Debug.Log(Time.time);
         if (request.isNetworkError || request.isHttpError)
         {
             Debug.Log(request.error);
             Debug.Log(request.downloadHandler.text);
         }
         else
         {
             Debug.Log(request.downloadHandler.text);
             ChatListing.text += request.downloadHandler.text;
         }

    }
    HasOpenUpdateRequest = false;
}

第二个是用来发送消息的:

private IEnumerator Send(string message)
{
    WWWForm formData = new WWWForm();

    formData.AddField("action", ServerAction.Chat);
    formData.AddField("sid", sessionID);
    formData.AddField("message", message);

    using (UnityWebRequest request = UnityWebRequest.Post("removed_url", formData))
    {
        UnityWebRequestAsyncOperation asyncOperation = request.SendWebRequest();
        yield return new WaitUntil(() => asyncOperation.isDone);

        if (request.isNetworkError || request.isHttpError)
        {
            Debug.Log(request.error);
            Debug.Log(request.downloadHandler.text);
        }
        else
        {
            Debug.Log(request.downloadHandler.text);
            ChatListing.text += $"{UserName}: {message}{Environment.NewLine}";
        }
    }
}

现在我观察到的行为:

enter image description here

现在我想要实现的目标:

enter image description here

我想服务器只是按顺序逐个处理请求... 现在,我需要什么来实现这种行为? 将Unity 2018.2与Mono和.Net 4.x以及后端的php 7.2结合使用。

0 个答案:

没有答案