2020-09-15 10:30:19 +03:00
|
|
|
extends Node
|
|
|
|
|
|
|
|
signal data_received(string)
|
|
|
|
|
|
|
|
export var port: int
|
|
|
|
|
|
|
|
var _s = TCP_Server.new()
|
|
|
|
var _c
|
|
|
|
var _connected = false
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
start()
|
|
|
|
|
|
|
|
func start():
|
|
|
|
_s.listen(port)
|
2020-09-24 11:10:14 +03:00
|
|
|
|
2020-09-15 13:36:22 +03:00
|
|
|
func _process(_delta):
|
2020-09-15 10:30:19 +03:00
|
|
|
if _s.is_connection_available():
|
|
|
|
if _connected:
|
2020-09-22 19:53:50 +03:00
|
|
|
_c.disconnect_from_host()
|
2020-09-15 10:30:19 +03:00
|
|
|
push_error("Dropping active connection")
|
|
|
|
_c = _s.take_connection()
|
|
|
|
_connected = true
|
|
|
|
print("connected!")
|
|
|
|
|
|
|
|
if _connected:
|
|
|
|
if _c.get_status() != StreamPeerTCP.STATUS_CONNECTED:
|
|
|
|
_connected = false
|
|
|
|
print("disconnected")
|
|
|
|
var available = _c.get_available_bytes()
|
|
|
|
while available > 0:
|
2020-09-24 11:10:14 +03:00
|
|
|
var data = _c.get_utf8_string(available)
|
2020-09-15 10:30:19 +03:00
|
|
|
emit_signal("data_received", data)
|
|
|
|
available = _c.get_available_bytes()
|
|
|
|
|
|
|
|
func send(text):
|
|
|
|
if _connected:
|
2020-09-24 11:10:14 +03:00
|
|
|
text += "\n"
|
|
|
|
_c.put_data(text.to_utf8())
|
2020-09-15 10:30:19 +03:00
|
|
|
else:
|
|
|
|
push_error("Trying to send data on closed connection")
|