Files
room_glitch/core/network/room_connection.gd
T

29 lines
1.3 KiB
GDScript

extends Node
## Transport boundary: gameplay and operator code only exchange command/state dictionaries.
var url := "http://127.0.0.1:8766"
var token := ""
func configure(role: String) -> void:
var config := ConfigFile.new()
config.load("user://arcade_network.cfg")
url = str(config.get_value("network", "url", url)).trim_suffix("/")
token = str(config.get_value("network", role + "_token", ""))
if OS.has_environment("ARCADE_ROOM_URL"): url = OS.get_environment("ARCADE_ROOM_URL").trim_suffix("/")
var name := "ARCADE_%s_TOKEN" % role.to_upper()
if OS.has_environment(name): token = OS.get_environment(name)
func post(path: String, payload: Dictionary = {}) -> Dictionary:
if token.is_empty(): return {"error": "Room connection token is not configured"}
var request := HTTPRequest.new()
request.timeout = 2.0
add_child(request)
var error := request.request(url + path, ["Content-Type: application/json", "Authorization: Bearer " + token], HTTPClient.METHOD_POST, JSON.stringify(payload))
var result: Dictionary = {"error": "Room controller is unreachable"}
if error == OK:
var response: Array = await request.request_completed
var body = JSON.parse_string(response[3].get_string_from_utf8())
if response[0] == HTTPRequest.RESULT_SUCCESS and body is Dictionary:
result = body
request.queue_free()
return result