Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node
- # HTTPClient
- var thread
- var mutex
- var semaphore
- var exit_thread = false
- var url_path = ""
- func _ready():
- mutex = Mutex.new()
- semaphore = Semaphore.new()
- exit_thread = false
- thread = Thread.new()
- thread.start(self, "_thread_function", "")
- func _thread_function(userdata):
- while true:
- semaphore.wait()
- mutex.lock()
- var should_exit = exit_thread
- mutex.unlock()
- if should_exit:
- break
- mutex.lock()
- _http_get_worker()
- mutex.unlock()
- func http_get(url_passed = ""):
- url_path = url_passed
- semaphore.post()
- func _exit_tree():
- mutex.lock()
- exit_thread = true
- mutex.unlock()
- semaphore.post()
- thread.wait_to_finish()
- func _http_get_worker():
- var err = 0
- var http = HTTPClient.new() # Create the Client.
- err = http.connect_to_host("localhost", 59125) # Connect to host/port.
- assert(err == OK) # Make sure connection was OK.
- # Wait until resolved and connected.
- while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
- http.poll()
- # print("Connecting...")
- OS.delay_msec(500)
- assert(http.get_status() == HTTPClient.STATUS_CONNECTED) # Could not connect
- # Some headers
- var headers = [
- "User-Agent: Pirulo/1.0 (Godot)",
- "Accept: */*"
- ]
- err = http.request(HTTPClient.METHOD_GET, url_path, headers) # Request a page from the site
- assert(err == OK) # Make sure all is OK.
- while http.get_status() == HTTPClient.STATUS_REQUESTING:
- # Keep polling for as long as the request is being processed.
- http.poll()
- # print("Requesting...")
- if not OS.has_feature("web"):
- OS.delay_msec(100)
- else:
- # Synchronous HTTP requests are not supported on the web,
- # so wait for the next main loop iteration.
- yield(Engine.get_main_loop(), "idle_frame")
- assert(http.get_status() == HTTPClient.STATUS_BODY or http.get_status() == HTTPClient.STATUS_CONNECTED) # Make sure request finished well.
- # print("response? ", http.has_response()) # Site might not have a response.
- if http.has_response():
- # If there is a response...
- headers = http.get_response_headers_as_dictionary() # Get response headers.
- # print("code: ", http.get_response_code()) # Show response code.
- # print("**headers:\\n", headers) # Show headers.
- # Getting the HTTP Body
- # if http.is_response_chunked():
- # # Does it use chunks?
- ## print("Response is Chunked!")
- # pass
- # else:
- # # Or just plain Content-Length
- # var bl = http.get_response_body_length()
- ## print("Response Length: ",bl)
- # This method works for both anyway
- var rb = PoolByteArray() # Array that will hold the data.
- 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.
- if chunk.size() == 0:
- # Got nothing, wait for buffers to fill a bit.
- OS.delay_usec(300)
- else:
- rb = rb + chunk # Append to read buffer.
- # Done!
- # print("bytes got: ", rb.size())
- var player = AudioStreamPlayer.new()
- self.add_child(player)
- var sound = AudioStreamSample.new()
- sound.data = rb
- sound.format = AudioStreamSample.FORMAT_16_BITS
- sound.loop_mode = AudioStreamSample.LOOP_DISABLED
- sound.stereo = false
- sound.mix_rate = 16000
- player.stream = sound
- player.play()
- # print("Ended")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement