Browse Source

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 years ago
parent
commit
2875c009fa
1 changed files with 11 additions and 5 deletions
  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:
         while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
             http.poll()
             http.poll()
             print("Connecting...")
             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
         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 http.get_status() == HTTPClient.STATUS_BODY:
                 # While there is body left to be read
                 # While there is body left to be read
                 http.poll()
                 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:
                 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:
                 else:
                     rb = rb + chunk # Append to read buffer.
                     rb = rb + chunk # Append to read buffer.
-
             # Done!
             # Done!
 
 
             print("bytes got: ", rb.size())
             print("bytes got: ", rb.size())