优化项目架构

This commit is contained in:
2025-09-15 19:10:37 +08:00
parent 4119ed3445
commit 26b856d74e
1361 changed files with 4 additions and 0 deletions

View File

@@ -0,0 +1,153 @@
extends Node
#一个基本的TCP客户端API
class_name TCPClient
signal connected_to_server#连接到服务器信号
signal connection_failed#连接失败信号
signal connection_closed#连接关闭信号
signal data_received(data)#收到数据信号
var tcp: StreamPeerTCP = StreamPeerTCP.new()
var host: String = "127.0.0.1"
var port: int = 4040
var is_connected: bool = false
var auto_reconnect: bool = true
var reconnect_delay: float = 2.0
# 缓冲区管理
var buffer = ""
func _ready():
pass
func _process(_delta):
# 更新连接状态
tcp.poll()
_update_connection_status()
_check_for_data()
func connect_to_server(custom_host = null, custom_port = null):
if custom_host != null:
host = custom_host
if custom_port != null:
port = custom_port
if tcp.get_status() != StreamPeerTCP.STATUS_CONNECTED:
tcp.disconnect_from_host()
print("连接到服务器: %s:%s" % [host, port])
var error = tcp.connect_to_host(host, port)
if error != OK:
print("连接错误: %s" % error)
emit_signal("connection_failed")
func disconnect_from_server():
tcp.disconnect_from_host()
is_connected = false
emit_signal("connection_closed")
func _update_connection_status():
var status = tcp.get_status()
match status:
StreamPeerTCP.STATUS_NONE:
if is_connected:
is_connected = false
print("连接已断开")
emit_signal("connection_closed")
if auto_reconnect:
var timer = get_tree().create_timer(reconnect_delay)
await timer.timeout
connect_to_server()
StreamPeerTCP.STATUS_CONNECTING:
pass
StreamPeerTCP.STATUS_CONNECTED:
if not is_connected:
is_connected = true
tcp.set_no_delay(true) # 禁用Nagle算法提高响应速度
emit_signal("connected_to_server")
StreamPeerTCP.STATUS_ERROR:
is_connected = false
print("连接错误")
emit_signal("connection_failed")
if auto_reconnect:
var timer = get_tree().create_timer(reconnect_delay)
await timer.timeout
connect_to_server()
func _check_for_data():
if tcp.get_status() == StreamPeerTCP.STATUS_CONNECTED and tcp.get_available_bytes() > 0:
var bytes = tcp.get_available_bytes()
var data = tcp.get_utf8_string(bytes)
# 将数据添加到缓冲区进行处理
buffer += data
_process_buffer()
func _process_buffer():
# 处理缓冲区中的JSON消息
# 假设每条消息以换行符结尾
while "\n" in buffer:
var message_end = buffer.find("\n")
var message_text = buffer.substr(0, message_end)
buffer = buffer.substr(message_end + 1)
# 处理JSON数据
if message_text.strip_edges() != "":
var json = JSON.new()
var error = json.parse(message_text)
if error == OK:
var data = json.get_data()
emit_signal("data_received", data)
else:
emit_signal("data_received", message_text)
func send_data(data):
if not is_connected:
print("未连接,无法发送数据")
return false
var message: String
# 如果是字典/数组转换为JSON
if typeof(data) == TYPE_DICTIONARY or typeof(data) == TYPE_ARRAY:
message = JSON.stringify(data) + "\n"
else:
# 否则简单转换为字符串
message = str(data) + "\n"
var result = tcp.put_data(message.to_utf8_buffer())
return result == OK
func is_client_connected() -> bool:
return is_connected
# 示例: 如何使用此客户端
#
# func _ready():
# var client = TCPClient.new()
# add_child(client)
#
# client.connected_to_server.connect(_on_connected)
# client.connection_failed.connect(_on_connection_failed)
# client.connection_closed.connect(_on_connection_closed)
# client.data_received.connect(_on_data_received)
#
# client.connect_to_server("127.0.0.1", 9000)
#
# func _on_connected():
# print("已连接")
# client.send_data({"type": "greeting", "content": "Hello Server!"})
#
# func _on_data_received(data):
# print("收到数据: ", data)

View File

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

File diff suppressed because it is too large Load Diff

View File

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

View File

@@ -0,0 +1,69 @@
[gd_scene load_steps=2 format=3 uid="uid://cpxiaqh0y6a5d"]
[ext_resource type="Script" uid="uid://q1f3tubmdsrk" path="res://Network/TCPNetworkManager.gd" id="1_tfd57"]
[node name="TCPNetworkManager" type="Panel"]
script = ExtResource("1_tfd57")
[node name="Scroll" type="ScrollContainer" parent="."]
layout_mode = 0
offset_left = 1.0
offset_top = 142.0
offset_right = 401.0
offset_bottom = 647.0
horizontal_scroll_mode = 0
[node name="ResponseLabel" type="Label" parent="Scroll"]
custom_minimum_size = Vector2(400, 500)
layout_mode = 2
theme_override_colors/font_outline_color = Color(1, 1, 1, 1)
theme_override_font_sizes/font_size = 20
text = "回应"
autowrap_mode = 3
[node name="Title" type="Label" parent="."]
layout_mode = 0
offset_right = 400.0
offset_bottom = 42.0
theme_override_font_sizes/font_size = 30
text = "TCP网络调试面板"
horizontal_alignment = 1
[node name="ColorRect" type="ColorRect" parent="."]
layout_mode = 0
offset_right = 400.0
offset_bottom = 647.0
color = Color(0.104753, 0.146763, 0.23013, 0.427451)
[node name="StatusLabel" type="Label" parent="."]
layout_mode = 0
offset_top = 100.0
offset_right = 120.0
offset_bottom = 142.0
theme_override_font_sizes/font_size = 30
text = "连接状态"
[node name="MessageInput" type="LineEdit" parent="."]
layout_mode = 0
offset_left = 136.0
offset_top = 50.0
offset_right = 400.0
offset_bottom = 100.0
theme_override_font_sizes/font_size = 30
[node name="SendButton" type="Button" parent="."]
layout_mode = 0
offset_left = 68.0
offset_top = 50.0
offset_right = 136.0
offset_bottom = 100.0
theme_override_font_sizes/font_size = 30
text = "发送"
[node name="ConnectionButton" type="Button" parent="."]
layout_mode = 0
offset_top = 50.0
offset_right = 68.0
offset_bottom = 100.0
theme_override_font_sizes/font_size = 30
text = "连接"