浏览代码

Adjust HTTPClient example code to not break in HTML5 exports (#4738)

Fixes godotengine/godot#46857

Added supported wait periods after poll() for HTML5
This prevents warning about multiple polls in the same frame
and prevent hanging the game when fetching the body chunks
Divided by Zer0 4 年之前
父节点
当前提交
36c9302533
共有 1 个文件被更改,包括 11 次插入5 次删除
  1. 11 5
      tutorials/networking/http_client_class.rst

+ 11 - 5
tutorials/networking/http_client_class.rst

@@ -34,7 +34,10 @@ It will connect and fetch a website.
         while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
             http.poll()
             print("Connecting...")
-            OS.delay_msec(500)
+            if not OS.has_feature("web"):
+                OS.delay_msec(500)
+            else:
+                yield(Engine.get_main_loop(), "idle_frame")
 
         assert(http.get_status() == HTTPClient.STATUS_CONNECTED) # Could not connect
 
@@ -86,13 +89,16 @@ It will connect and fetch a website.
             while http.get_status() == HTTPClient.STATUS_BODY:
                 # While there is body left to be read
                 http.poll()
-                var chunk = http.read_response_body_chunk() # Get a chunk.
+                # Get a chunk.
+                var chunk = http.read_response_body_chunk()
                 if chunk.size() == 0:
-                    # Got nothing, wait for buffers to fill a bit.
-                    OS.delay_usec(1000)
+                    if not OS.has_feature("web"):
+                        # Got nothing, wait for buffers to fill a bit.
+                        OS.delay_usec(1000)
+                    else:
+                        yield(Engine.get_main_loop(), "idle_frame")
                 else:
                     rb = rb + chunk # Append to read buffer.
-
             # Done!
 
             print("bytes got: ", rb.size())