HTTPClient Test
HTTPClient Test Code.
This commit is contained in:
parent
eacd8d72af
commit
5b3c74f0be
3 changed files with 59 additions and 0 deletions
52
http_client.gd
Normal file
52
http_client.gd
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
extends Node2D
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
var client := HTTPClient.new()
|
||||
var res = client.connect_to_host("127.0.0.1",9450)
|
||||
|
||||
assert(res == OK)
|
||||
|
||||
while client.get_status() == HTTPClient.STATUS_CONNECTING or client.get_status() == HTTPClient.STATUS_RESOLVING:
|
||||
client.poll()
|
||||
print("Connecting...")
|
||||
await get_tree().process_frame
|
||||
|
||||
assert(client.get_status() == HTTPClient.STATUS_CONNECTED)
|
||||
|
||||
res = client.request(HTTPClient.METHOD_GET, "/api?request=getVariable&name=twitch_default_channel", [])
|
||||
assert(res == OK)
|
||||
|
||||
while client.get_status() == HTTPClient.STATUS_REQUESTING:
|
||||
client.poll()
|
||||
print("Requesting...")
|
||||
await get_tree().process_frame
|
||||
|
||||
assert(client.get_status() == HTTPClient.STATUS_BODY or client.get_status() == HTTPClient.STATUS_CONNECTED)
|
||||
|
||||
print("response? ", client.has_response())
|
||||
|
||||
if client.has_response():
|
||||
var headers := client.get_response_headers_as_dictionary()
|
||||
print("code: ", client.get_response_code())
|
||||
print("**headers:\\n", headers)
|
||||
|
||||
if client.is_response_chunked():
|
||||
print("Response is Chunked!")
|
||||
else:
|
||||
var bl := client.get_response_body_length()
|
||||
print("Response Length: ", bl)
|
||||
var rb := PackedByteArray()
|
||||
|
||||
while client.get_status() == HTTPClient.STATUS_BODY:
|
||||
client.poll()
|
||||
var chunk = client.read_response_body_chunk()
|
||||
if chunk.size() == 0:
|
||||
await get_tree().process_frame
|
||||
else:
|
||||
rb = rb + chunk
|
||||
|
||||
print("bytes got: ", rb.size())
|
||||
var text = rb.get_string_from_ascii()
|
||||
print("Text: ", text)
|
||||
1
http_client.gd.uid
Normal file
1
http_client.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://d18wt8smvaeid
|
||||
6
http_client.tscn
Normal file
6
http_client.tscn
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[gd_scene load_steps=2 format=3 uid="uid://d247bxhjaip8h"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://d18wt8smvaeid" path="res://http_client.gd" id="1_4bxm2"]
|
||||
|
||||
[node name="HttpClient" type="Node2D"]
|
||||
script = ExtResource("1_4bxm2")
|
||||
Loading…
Add table
Add a link
Reference in a new issue