-
-
Save bonekost/7765cb893f0ff3e7ca45d2e128c68a59 to your computer and use it in GitHub Desktop.
Godot HTTPClient usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## rpc.gd | |
# An experiment in using HTTPClient | |
# for blocking RPCs | |
# @author bibby<[email protected]> | |
# | |
# get( url ) | |
# post( url, body ) | |
# put( url, body ) | |
# delete( url ) | |
extends Node | |
const ERROR = 1 | |
const OK = 0 | |
# TODO, setter for domain | |
var _host = "localhost" | |
var _port = 80 | |
var _error = "" | |
var _response = "" | |
var client = HTTPClient.new() | |
func get(url): | |
return _request( HTTPClient.METHOD_GET, url, "" ) | |
# TODO, form encode collections if desired | |
func post(url, body): | |
return _request( HTTPClient.METHOD_POST, url, body) | |
func put(url, body): | |
return _request( HTTPClient.METHOD_PUT, url, body) | |
func delete(url): | |
return _request( HTTPClient.METHOD_DELETE, url, "" ) | |
func _request(method, url, body): | |
_response = "" | |
var res = _connect() | |
if( res == ERROR ): | |
return _getErr() | |
else: | |
client.request( method, url, StringArray(["User-Agent: godot game engine"]), body) | |
# TODO, Content-Type and other headers | |
res = _poll() | |
if( res != OK ): | |
return _getErr() | |
client.close() | |
return _response | |
func _connect(): | |
client.connect(_host, _port) | |
var res = _poll() | |
if( res != OK ): | |
return ERROR | |
return OK | |
func _getErr(): | |
return _error | |
func _setError(msg): | |
_error = str(msg) | |
return ERROR | |
func _poll(): | |
var status = -1 | |
var current_status | |
while(true): | |
client.poll() | |
current_status = client.get_status() | |
if( status != current_status ): | |
status = current_status | |
print("HTTPClient entered status ", status) | |
if( status == HTTPClient.STATUS_RESOLVING ): | |
continue | |
if( status == HTTPClient.STATUS_REQUESTING ): | |
continue | |
if( status == HTTPClient.STATUS_CONNECTING ): | |
continue | |
if( status == HTTPClient.STATUS_CONNECTED ): | |
return OK | |
if( status == HTTPClient.STATUS_DISCONNECTED ): | |
return _setError("Disconnected from Host") | |
if( status == HTTPClient.STATUS_CANT_RESOLVE ): | |
return _setError("Can't Resolve Host") | |
if( status == HTTPClient.STATUS_CANT_CONNECT ): | |
return _setError("Can't Connect to Host") | |
if( status == HTTPClient.STATUS_CONNECTION_ERROR ): | |
return _setError("Connection Error") | |
if( status == HTTPClient.STATUS_BODY ): | |
return _parseBody() | |
func _parseBody(): | |
_response = client.read_response_body_chunk().get_string_from_ascii() | |
var response_code = client.get_response_code() | |
if( response_code >= 200 && response_code < 300 ): | |
return OK | |
if( response_code >= 400 ): | |
return _setError( "HTTP:" + str(response_code) ) | |
return OK | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# modified sayhello.gd example | |
extends Panel | |
var RPC = preload("rpc.gd") | |
var _rpc | |
func _init(): | |
_rpc = RPC.new() | |
func _ready(): | |
get_node("Button").connect("pressed", self, "onButtonPress") | |
func onButtonPress(): | |
get_node("Label").set_text( str(_rpc.get("/godot.php")) ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment