Advertisement
Guest User

Untitled

a guest
Sep 28th, 2019
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. extends Node
  2.  
  3. # HTTPClient
  4.  
  5. var thread
  6. var mutex
  7. var semaphore
  8. var exit_thread = false
  9. var url_path = ""
  10.  
  11.  
  12. func _ready():
  13. mutex = Mutex.new()
  14. semaphore = Semaphore.new()
  15. exit_thread = false
  16.  
  17. thread = Thread.new()
  18. thread.start(self, "_thread_function", "")
  19.  
  20.  
  21. func _thread_function(userdata):
  22. while true:
  23. semaphore.wait()
  24.  
  25. mutex.lock()
  26. var should_exit = exit_thread
  27. mutex.unlock()
  28.  
  29. if should_exit:
  30. break
  31.  
  32. mutex.lock()
  33. _http_get_worker()
  34. mutex.unlock()
  35.  
  36.  
  37. func http_get(url_passed = ""):
  38. url_path = url_passed
  39. semaphore.post()
  40.  
  41.  
  42. func _exit_tree():
  43. mutex.lock()
  44. exit_thread = true
  45. mutex.unlock()
  46.  
  47. semaphore.post()
  48.  
  49. thread.wait_to_finish()
  50.  
  51.  
  52. func _http_get_worker():
  53. var err = 0
  54. var http = HTTPClient.new() # Create the Client.
  55.  
  56. err = http.connect_to_host("localhost", 59125) # Connect to host/port.
  57. assert(err == OK) # Make sure connection was OK.
  58.  
  59. # Wait until resolved and connected.
  60. while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
  61. http.poll()
  62. # print("Connecting...")
  63. OS.delay_msec(500)
  64.  
  65. assert(http.get_status() == HTTPClient.STATUS_CONNECTED) # Could not connect
  66.  
  67. # Some headers
  68. var headers = [
  69. "User-Agent: Pirulo/1.0 (Godot)",
  70. "Accept: */*"
  71. ]
  72.  
  73. err = http.request(HTTPClient.METHOD_GET, url_path, headers) # Request a page from the site
  74. assert(err == OK) # Make sure all is OK.
  75.  
  76. while http.get_status() == HTTPClient.STATUS_REQUESTING:
  77. # Keep polling for as long as the request is being processed.
  78. http.poll()
  79. # print("Requesting...")
  80. if not OS.has_feature("web"):
  81. OS.delay_msec(100)
  82. else:
  83. # Synchronous HTTP requests are not supported on the web,
  84. # so wait for the next main loop iteration.
  85. yield(Engine.get_main_loop(), "idle_frame")
  86.  
  87. assert(http.get_status() == HTTPClient.STATUS_BODY or http.get_status() == HTTPClient.STATUS_CONNECTED) # Make sure request finished well.
  88.  
  89. # print("response? ", http.has_response()) # Site might not have a response.
  90.  
  91. if http.has_response():
  92. # If there is a response...
  93.  
  94. headers = http.get_response_headers_as_dictionary() # Get response headers.
  95. # print("code: ", http.get_response_code()) # Show response code.
  96. # print("**headers:\\n", headers) # Show headers.
  97.  
  98. # Getting the HTTP Body
  99.  
  100. # if http.is_response_chunked():
  101. # # Does it use chunks?
  102. ## print("Response is Chunked!")
  103. # pass
  104. # else:
  105. # # Or just plain Content-Length
  106. # var bl = http.get_response_body_length()
  107. ## print("Response Length: ",bl)
  108.  
  109. # This method works for both anyway
  110.  
  111. var rb = PoolByteArray() # Array that will hold the data.
  112.  
  113. while http.get_status() == HTTPClient.STATUS_BODY:
  114. # While there is body left to be read
  115. http.poll()
  116. var chunk = http.read_response_body_chunk() # Get a chunk.
  117. if chunk.size() == 0:
  118. # Got nothing, wait for buffers to fill a bit.
  119. OS.delay_usec(300)
  120. else:
  121. rb = rb + chunk # Append to read buffer.
  122.  
  123. # Done!
  124.  
  125. # print("bytes got: ", rb.size())
  126.  
  127. var player = AudioStreamPlayer.new()
  128. self.add_child(player)
  129. var sound = AudioStreamSample.new()
  130. sound.data = rb
  131. sound.format = AudioStreamSample.FORMAT_16_BITS
  132. sound.loop_mode = AudioStreamSample.LOOP_DISABLED
  133. sound.stereo = false
  134. sound.mix_rate = 16000
  135. player.stream = sound
  136. player.play()
  137.  
  138. # print("Ended")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement