Skip to content

Instantly share code, notes, and snippets.

@pegasusearl
Last active December 21, 2018 09:07
Show Gist options
  • Select an option

  • Save pegasusearl/717f8098e4364a06e5521d5bb300bb00 to your computer and use it in GitHub Desktop.

Select an option

Save pegasusearl/717f8098e4364a06e5521d5bb300bb00 to your computer and use it in GitHub Desktop.
extends Node
# === Server
onready var auth = TCP_Server.new()
func _ready():
#from https://docs.godotengine.org/en/3.0/classes/class_tcp_server.html
auth.listen(5712,"*")
pass
func _process(delta):
if auth.is_connection_available():
var listen = auth.take_connection()
print(listen)
answer_that_request("sure, now take it!!")
func answer_that_request(message):
#How do I do that?
pass
# === Client
#From official docs https://docs.godotengine.org/en/3.0/tutorials/networking/http_request_class.html
func _on_HTTPRequest_request_completed(result, response_code, headers, body):
var json = JSON.parse(body.get_string_from_utf8())
# print(json.result)
pass # replace with function body
#for testing purpose (and to communicate what I tried to achieve)
func _on_Timer_timeout():
_make_post_request("http://127.0.0.1:5712","Hello, can you give me tea?",false)
pass
#From official docs https://docs.godotengine.org/en/3.0/tutorials/networking/http_request_class.html
func _make_post_request(url, data_to_send, use_ssl):
# Convert data to json string:
var query = JSON.print(data_to_send)
# Add 'Content-Type' header:
var headers = ["Content-Type: application/json"]
$HTTPRequest.request(url, headers, use_ssl, HTTPClient.METHOD_POST, query)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment