Working Code

Create a Plugin with this code, for Working Sammi Socket Connection.
This commit is contained in:
Mario Steele 2025-04-30 04:09:20 -05:00
parent 03c0c804b0
commit eacd8d72af
5 changed files with 33 additions and 5 deletions

18
test_godot.gd Normal file
View file

@ -0,0 +1,18 @@
extends Node2D
@onready var http_request: HTTPRequest = $HTTPRequest
func _ready() -> void:
http_request.request_completed.connect(_request_data)
http_request.request("http://localhost:9450/api?getVariable=somevariable")
func _request_data(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray) -> void:
print("Result: %d" % result)
print("Response Code: %d" % response_code)
print("Headers:", JSON.stringify(headers, "\t"))
var data := body.get_string_from_utf8()
var d: Dictionary = JSON.parse_string(data)
print("Body (str): ", body.get_string_from_utf8())
print("Body (JSON): ", JSON.stringify(d, "\t"))
print("Completed")

1
test_godot.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://byipe2kjrnbqu

9
test_godot.tscn Normal file
View file

@ -0,0 +1,9 @@
[gd_scene load_steps=2 format=3 uid="uid://bs53go6esgusj"]
[ext_resource type="Script" uid="uid://byipe2kjrnbqu" path="res://test_godot.gd" id="1_pyjj8"]
[node name="TestGodot" type="Node2D"]
script = ExtResource("1_pyjj8")
[node name="HTTPRequest" type="HTTPRequest" parent="."]
accept_gzip = false

View file

@ -90,7 +90,7 @@ func _send_request(peer: StreamPeer) -> void:
if ssl: if ssl:
proto = "https" proto = "https"
var request_str := "GET %s://%s:%d%s HTTP/1.1\r\n" % [proto, host, port, query] var request_str := "GET %s HTTP/1.1\r\n" % [query]
if (ssl and port == 443) or (not ssl and port == 80): if (ssl and port == 443) or (not ssl and port == 80):
request_str += "Host: %s\r\n" % host request_str += "Host: %s\r\n" % host
else: else:
@ -108,7 +108,7 @@ func _send_request(peer: StreamPeer) -> void:
] ]
request_str += "Accept: %s\r\n\r\n" % accept request_str += "Accept: %s\r\n\r\n" % accept
print_rich("[color=cyan]Request Headers:",request_str,"[/color]--EOH--") print_rich("[color=cyan]Request Headers:\n",request_str,"[/color]--EOH--")
peer.put_data(request_str.to_utf8_buffer()) peer.put_data(request_str.to_utf8_buffer())
func _handle_response(peer: StreamPeer) -> void: func _handle_response(peer: StreamPeer) -> void:

View file

@ -4,6 +4,6 @@
[node name="TestNode" type="Node2D"] [node name="TestNode" type="Node2D"]
script = ExtResource("1_6uery") script = ExtResource("1_6uery")
host = "api.github.com" host = "localhost"
port = 443 port = 9450
query = "/repos/godotengine/godot/releases/latest" query = "/api?getVariable=somevariable"