临时
1
This commit is contained in:
@@ -17,14 +17,27 @@ var client: TCPClient = TCPClient.new()
|
||||
|
||||
# 服务器配置 - 支持多个服务器地址
|
||||
var server_configs = [
|
||||
{"host": "127.0.0.1", "port": 4040, "name": "本地服务器"},
|
||||
#{"host": "192.168.1.110", "port": 4040, "name": "局域网服务器"},
|
||||
#{"host": "47.108.90.0", "port": 4040, "name": "公网服务器"}#成都内网穿透
|
||||
#{"host": "127.0.0.1", "port": 4040, "name": "本地"},
|
||||
#{"host": "192.168.1.110", "port": 4040, "name": "局域网"},
|
||||
#{"host": "47.108.90.0", "port": 4040, "name": "成都内网穿透"}#成都内网穿透
|
||||
{"host": "47.108.90.0", "port": 6060, "name": "成都公网"}#成都服务器
|
||||
]
|
||||
|
||||
var current_server_index = 0
|
||||
var auto_retry = true
|
||||
var retry_delay = 3.0
|
||||
var connection_timeout = 5.0 # 连接超时时间
|
||||
var is_trying_to_connect = false
|
||||
var connection_start_time = 0.0
|
||||
var has_tried_all_servers = false # 是否已尝试过所有服务器
|
||||
|
||||
# 延迟测量相关变量
|
||||
var ping_start_time = 0.0
|
||||
var current_ping = -1 # -1表示尚未测量
|
||||
var ping_timer = 0.0
|
||||
var ping_interval = 3.0 # 每3秒ping一次
|
||||
var ping_timeout = 5.0 # ping超时时间
|
||||
var is_measuring_ping = false
|
||||
|
||||
func _ready():
|
||||
# 创建TCP客户端实例
|
||||
@@ -41,14 +54,71 @@ func _ready():
|
||||
send_button.pressed.connect(_on_send_button_pressed)
|
||||
|
||||
# 初始设置
|
||||
status_label.text = "未连接"
|
||||
status_label.text = "❌ 未连接"
|
||||
status_label.modulate = Color.RED
|
||||
response_label.text = "等待响应..."
|
||||
connection_button.text = "连接"
|
||||
|
||||
# 初始化延迟测量变量
|
||||
current_ping = -1
|
||||
is_measuring_ping = false
|
||||
ping_timer = 0.0
|
||||
|
||||
# 每帧检查连接状态和超时
|
||||
func _process(delta):
|
||||
# 检查连接超时
|
||||
if is_trying_to_connect:
|
||||
var elapsed_time = Time.get_unix_time_from_system() - connection_start_time
|
||||
if elapsed_time > connection_timeout:
|
||||
print("连接超时,尝试下一个服务器")
|
||||
is_trying_to_connect = false
|
||||
client.disconnect_from_server()
|
||||
|
||||
if auto_retry and not has_tried_all_servers:
|
||||
try_next_server()
|
||||
else:
|
||||
status_label.text = "服务器连接失败"
|
||||
status_label.modulate = Color.RED
|
||||
has_tried_all_servers = false # 重置标志,允许下次重试
|
||||
|
||||
# 处理延迟测量
|
||||
if client.is_client_connected():
|
||||
ping_timer += delta
|
||||
if ping_timer >= ping_interval and not is_measuring_ping:
|
||||
ping_timer = 0.0
|
||||
send_ping()
|
||||
|
||||
# 检查ping超时
|
||||
if is_measuring_ping:
|
||||
var ping_elapsed = Time.get_unix_time_from_system() - ping_start_time
|
||||
if ping_elapsed > ping_timeout:
|
||||
print("Ping超时,重置测量状态")
|
||||
is_measuring_ping = false
|
||||
current_ping = 999 # 显示为高延迟
|
||||
|
||||
# 更新状态显示
|
||||
update_connection_status()
|
||||
else:
|
||||
# 未连接时重置延迟相关状态
|
||||
current_ping = -1
|
||||
is_measuring_ping = false
|
||||
ping_timer = 0.0
|
||||
|
||||
# 更新状态显示
|
||||
update_connection_status()
|
||||
|
||||
func _on_connected():
|
||||
status_label.text = "已连接"
|
||||
print("成功连接到服务器: ", server_configs[current_server_index]["name"])
|
||||
status_label.text = "已连接 测量中..."
|
||||
status_label.modulate = Color.GREEN
|
||||
connection_button.text = "断开"
|
||||
is_trying_to_connect = false
|
||||
has_tried_all_servers = false # 连接成功后重置标志
|
||||
|
||||
# 重置延迟测量
|
||||
current_ping = -1
|
||||
ping_timer = 0.0
|
||||
is_measuring_ping = false
|
||||
|
||||
# 发送连接成功消息
|
||||
client.send_data({
|
||||
@@ -59,27 +129,58 @@ func _on_connected():
|
||||
# 连接成功后立即请求作物数据
|
||||
print("连接成功,正在请求最新作物数据...")
|
||||
sendGetCropData()
|
||||
|
||||
# 连接成功后立即请求在线人数
|
||||
print("连接成功,正在请求在线人数...")
|
||||
sendGetOnlinePlayers()
|
||||
|
||||
# 立即开始第一次ping测量
|
||||
send_ping()
|
||||
|
||||
func _on_connection_failed():
|
||||
status_label.text = "连接失败"
|
||||
print("连接失败: ", server_configs[current_server_index]["name"])
|
||||
status_label.text = "连接失败 - " + server_configs[current_server_index]["name"]
|
||||
status_label.modulate = Color.RED
|
||||
connection_button.text = "连接"
|
||||
is_trying_to_connect = false
|
||||
|
||||
# 重置延迟测量
|
||||
current_ping = -1
|
||||
is_measuring_ping = false
|
||||
ping_timer = 0.0
|
||||
|
||||
# 通知主游戏更新在线人数显示
|
||||
if main_game and main_game.has_method("_update_online_players_display"):
|
||||
main_game._update_online_players_display(0, false, false)
|
||||
|
||||
# 自动尝试下一个服务器
|
||||
if auto_retry:
|
||||
try_next_server()
|
||||
|
||||
func _on_connection_closed():
|
||||
status_label.text = "连接断开"
|
||||
print("连接断开: ", server_configs[current_server_index]["name"])
|
||||
status_label.text = "连接断开 "
|
||||
status_label.modulate = Color.RED
|
||||
connection_button.text = "连接"
|
||||
is_trying_to_connect = false
|
||||
|
||||
# 自动重连当前服务器
|
||||
# 重置延迟测量
|
||||
current_ping = -1
|
||||
is_measuring_ping = false
|
||||
ping_timer = 0.0
|
||||
|
||||
# 通知主游戏更新在线人数显示
|
||||
if main_game and main_game.has_method("_update_online_players_display"):
|
||||
main_game._update_online_players_display(0, false, false)
|
||||
|
||||
# 自动重连,优先尝试其他服务器
|
||||
if auto_retry:
|
||||
print("连接断开,准备重连...")
|
||||
var timer = get_tree().create_timer(retry_delay)
|
||||
await timer.timeout
|
||||
if not client.is_client_connected():
|
||||
_on_connection_button_pressed()
|
||||
# 先尝试下一个服务器,如果所有服务器都失败了再回到当前服务器
|
||||
try_next_server()
|
||||
|
||||
func _on_data_received(data):
|
||||
# 根据数据类型处理数据
|
||||
@@ -91,67 +192,212 @@ func _on_data_received(data):
|
||||
var message_type = data.get("type", "")
|
||||
|
||||
match message_type:
|
||||
"ping":
|
||||
"ping": #ping是否在线
|
||||
return
|
||||
"response":
|
||||
"pong": #服务器回复pong响应
|
||||
handle_pong_response()
|
||||
return
|
||||
"login_response":
|
||||
# 处理登录响应
|
||||
"response": #服务器通用响应
|
||||
return
|
||||
"login_response": #登录响应
|
||||
var status = data.get("status", "")
|
||||
var message = data.get("message", "")
|
||||
var player_data = data.get("player_data", {})
|
||||
if login_panel:
|
||||
# 调用登录面板的响应处理方法
|
||||
login_panel._on_login_response_received(status == "success", message, player_data)
|
||||
"register_response":
|
||||
# 处理注册响应
|
||||
"register_response": #注册响应
|
||||
var status = data.get("status", "")
|
||||
var message = data.get("message", "")
|
||||
if login_panel:
|
||||
# 调用登录面板的响应处理方法
|
||||
login_panel._on_register_response_received(status == "success", message)
|
||||
"verification_code_response":
|
||||
# 处理验证码发送响应
|
||||
"verification_code_response": #验证码发送响应
|
||||
var success = data.get("success", false)
|
||||
var message = data.get("message", "")
|
||||
if login_panel:
|
||||
# 调用登录面板的验证码响应处理方法
|
||||
login_panel._on_verification_code_response(success, message)
|
||||
"verify_code_response":
|
||||
# 处理验证码验证响应
|
||||
"verify_code_response": #验证码验证响应
|
||||
var success = data.get("success", false)
|
||||
var message = data.get("message", "")
|
||||
if login_panel:
|
||||
# 调用登录面板的验证码验证响应处理方法
|
||||
login_panel._on_verify_code_response(success, message)
|
||||
"crop_update":
|
||||
# 处理作物生长更新
|
||||
"crop_update": #作物更新响应
|
||||
if main_game:
|
||||
main_game._handle_crop_update(data)
|
||||
"action_response":
|
||||
# 处理玩家动作响应
|
||||
"action_response": #玩家操作响应
|
||||
if main_game:
|
||||
main_game._handle_action_response(data)
|
||||
"play_time_response":
|
||||
# 处理玩家游玩时间响应
|
||||
# 处理玩家动作到服务端响应消息
|
||||
var action_type = data.get("action_type", "")
|
||||
var success = data.get("success", false)
|
||||
var message = data.get("message", "")
|
||||
var updated_data = data.get("updated_data", {})
|
||||
|
||||
match action_type:
|
||||
"harvest_crop":#处理收获作物响应
|
||||
if success:
|
||||
# 更新玩家数据
|
||||
if updated_data.has("money"):
|
||||
main_game.money = updated_data["money"]
|
||||
if updated_data.has("experience"):
|
||||
main_game.experience = updated_data["experience"]
|
||||
if updated_data.has("level"):
|
||||
main_game.level = updated_data["level"]
|
||||
if updated_data.has("体力值"):
|
||||
main_game.stamina = updated_data["体力值"]
|
||||
|
||||
# 更新UI
|
||||
main_game._update_ui()
|
||||
Toast.show(message, Color.GREEN)
|
||||
else:
|
||||
Toast.show(message, Color.RED)
|
||||
"plant_crop":#处理种植作物响应
|
||||
if success:
|
||||
# 更新玩家背包
|
||||
if updated_data.has("player_bag"):
|
||||
main_game.player_bag = updated_data["player_bag"]
|
||||
|
||||
# 更新玩家背包UI
|
||||
main_game.player_bag_panel.update_player_bag_ui()
|
||||
Toast.show(message, Color.GREEN)
|
||||
else:
|
||||
Toast.show(message, Color.RED)
|
||||
"buy_seed":#处理购买种子响应
|
||||
if success:
|
||||
# 更新玩家数据
|
||||
if updated_data.has("money"):
|
||||
main_game.money = updated_data["money"]
|
||||
if updated_data.has("player_bag"):
|
||||
main_game.player_bag = updated_data["player_bag"]
|
||||
|
||||
# 更新UI
|
||||
main_game._update_ui()
|
||||
main_game.player_bag_panel.update_player_bag_ui()
|
||||
Toast.show(message, Color.GREEN)
|
||||
else:
|
||||
Toast.show(message, Color.RED)
|
||||
"dig_ground":#处理开垦土地
|
||||
if success:
|
||||
# 更新玩家数据
|
||||
if updated_data.has("money"):
|
||||
main_game.money = updated_data["money"]
|
||||
if updated_data.has("farm_lots"):
|
||||
main_game.farm_lots = updated_data["farm_lots"]
|
||||
|
||||
# 更新UI
|
||||
main_game._update_ui()
|
||||
main_game._update_farm_lots_state()
|
||||
Toast.show(message, Color.GREEN)
|
||||
else:
|
||||
Toast.show(message, Color.RED)
|
||||
"remove_crop":#处理铲除作物
|
||||
if success:
|
||||
# 更新玩家数据
|
||||
if updated_data.has("money"):
|
||||
main_game.money = updated_data["money"]
|
||||
if updated_data.has("farm_lots"):
|
||||
main_game.farm_lots = updated_data["farm_lots"]
|
||||
|
||||
# 更新UI
|
||||
main_game._update_ui()
|
||||
main_game._update_farm_lots_state()
|
||||
Toast.show(message, Color.GREEN)
|
||||
else:
|
||||
Toast.show(message, Color.RED)
|
||||
"water_crop":#处理浇水
|
||||
if success:
|
||||
# 更新玩家数据
|
||||
if updated_data.has("money"):
|
||||
main_game.money = updated_data["money"]
|
||||
if updated_data.has("farm_lots"):
|
||||
main_game.farm_lots = updated_data["farm_lots"]
|
||||
if updated_data.has("体力值"):
|
||||
main_game.stamina = updated_data["体力值"]
|
||||
|
||||
# 更新UI
|
||||
main_game._update_ui()
|
||||
main_game._update_farm_lots_state()
|
||||
Toast.show(message, Color.CYAN)
|
||||
else:
|
||||
Toast.show(message, Color.RED)
|
||||
"fertilize_crop":#处理施肥
|
||||
if success:
|
||||
# 更新玩家数据
|
||||
if updated_data.has("money"):
|
||||
main_game.money = updated_data["money"]
|
||||
if updated_data.has("farm_lots"):
|
||||
main_game.farm_lots = updated_data["farm_lots"]
|
||||
if updated_data.has("体力值"):
|
||||
main_game.stamina = updated_data["体力值"]
|
||||
|
||||
# 更新UI
|
||||
main_game._update_ui()
|
||||
main_game._update_farm_lots_state()
|
||||
Toast.show(message, Color.PURPLE)
|
||||
else:
|
||||
Toast.show(message, Color.RED)
|
||||
"upgrade_land":#处理升级土地
|
||||
if success:
|
||||
# 更新玩家数据
|
||||
if updated_data.has("money"):
|
||||
main_game.money = updated_data["money"]
|
||||
if updated_data.has("farm_lots"):
|
||||
main_game.farm_lots = updated_data["farm_lots"]
|
||||
|
||||
# 更新UI
|
||||
main_game._update_ui()
|
||||
main_game._update_farm_lots_state()
|
||||
Toast.show(message, Color.GOLD)
|
||||
else:
|
||||
Toast.show(message, Color.RED)
|
||||
"buy_new_ground":#处理添加新土地
|
||||
if success:
|
||||
# 更新玩家数据
|
||||
if updated_data.has("money"):
|
||||
main_game.money = updated_data["money"]
|
||||
if updated_data.has("farm_lots"):
|
||||
main_game.farm_lots = updated_data["farm_lots"]
|
||||
|
||||
# 重新创建UI来显示新地块
|
||||
main_game._create_farm_buttons()
|
||||
main_game._update_farm_lots_state()
|
||||
main_game._update_ui()
|
||||
Toast.show(message, Color.GREEN)
|
||||
else:
|
||||
Toast.show(message, Color.RED)
|
||||
"play_time_response": #游玩时间统计响应
|
||||
if main_game and main_game.has_method("_handle_play_time_response"):
|
||||
main_game._handle_play_time_response(data)
|
||||
"player_rankings_response":
|
||||
# 处理玩家排行榜响应
|
||||
"player_rankings_response": #玩家排行榜响应
|
||||
if main_game and main_game.has_method("_handle_player_rankings_response"):
|
||||
main_game._handle_player_rankings_response(data)
|
||||
"crop_data_response":
|
||||
# 处理作物数据响应
|
||||
"crop_data_response": #作物数据更新响应
|
||||
if main_game and main_game.has_method("_handle_crop_data_response"):
|
||||
main_game._handle_crop_data_response(data)
|
||||
"visit_player_response":
|
||||
# 处理访问玩家响应
|
||||
"visit_player_response": #访问玩家响应
|
||||
if main_game and main_game.has_method("_handle_visit_player_response"):
|
||||
main_game._handle_visit_player_response(data)
|
||||
"return_my_farm_response":
|
||||
# 处理返回自己农场响应
|
||||
"return_my_farm_response": #返回我的农场响应
|
||||
if main_game and main_game.has_method("_handle_return_my_farm_response"):
|
||||
main_game._handle_return_my_farm_response(data)
|
||||
"like_player_response": #点赞玩家响应
|
||||
if main_game and main_game.has_method("_handle_like_player_response"):
|
||||
main_game._handle_like_player_response(data)
|
||||
"online_players_response": #玩家在线响应
|
||||
if main_game and main_game.has_method("_handle_online_players_response"):
|
||||
main_game._handle_online_players_response(data)
|
||||
"daily_check_in_response": #每日签到响应
|
||||
if main_game and main_game.has_method("_handle_daily_check_in_response"):
|
||||
main_game._handle_daily_check_in_response(data)
|
||||
"check_in_data_response": #获取签到数据响应
|
||||
if main_game and main_game.has_method("_handle_check_in_data_response"):
|
||||
main_game._handle_check_in_data_response(data)
|
||||
"lucky_draw_response": #幸运抽奖响应
|
||||
if main_game and main_game.has_method("_handle_lucky_draw_response"):
|
||||
main_game._handle_lucky_draw_response(data)
|
||||
"new_player_gift_response": #新手大礼包响应
|
||||
if main_game and main_game.has_method("_handle_new_player_gift_response"):
|
||||
main_game._handle_new_player_gift_response(data)
|
||||
"pong": #延迟检测响应
|
||||
handle_pong_response(data)
|
||||
_:
|
||||
# 显示其他类型的消息
|
||||
return
|
||||
@@ -163,10 +409,28 @@ func _on_connection_button_pressed():
|
||||
if client.is_client_connected():
|
||||
# 断开连接
|
||||
client.disconnect_from_server()
|
||||
is_trying_to_connect = false
|
||||
has_tried_all_servers = false
|
||||
else:
|
||||
# 连接服务器
|
||||
status_label.text = "正在连接..."
|
||||
client.connect_to_server(server_configs[current_server_index]["host"], server_configs[current_server_index]["port"])
|
||||
# 连接服务器,从当前服务器开始尝试
|
||||
has_tried_all_servers = false
|
||||
connect_to_current_server()
|
||||
|
||||
# 连接到当前选择的服务器
|
||||
func connect_to_current_server():
|
||||
var config = server_configs[current_server_index]
|
||||
status_label.text = "正在连接 " + config["name"] + "..."
|
||||
status_label.modulate = Color.YELLOW
|
||||
|
||||
print("=== 尝试连接服务器 ===")
|
||||
print("服务器名称: ", config["name"])
|
||||
print("服务器地址: ", config["host"], ":", config["port"])
|
||||
print("服务器索引: ", current_server_index, "/", server_configs.size() - 1)
|
||||
|
||||
is_trying_to_connect = true
|
||||
connection_start_time = Time.get_unix_time_from_system()
|
||||
|
||||
client.connect_to_server(config["host"], config["port"])
|
||||
|
||||
func _on_send_button_pressed():
|
||||
if not client.is_client_connected():
|
||||
@@ -210,13 +474,14 @@ func sendRegisterInfo(username, password, farmname, player_name="", verification
|
||||
})
|
||||
|
||||
#发送收获作物信息
|
||||
func sendHarvestCrop(lot_index):
|
||||
func sendHarvestCrop(lot_index, target_username = ""):
|
||||
if not client.is_client_connected():
|
||||
return false
|
||||
|
||||
client.send_data({
|
||||
"type": "harvest_crop",
|
||||
"lot_index": lot_index,
|
||||
"target_username": target_username,
|
||||
"timestamp": Time.get_unix_time_from_system()
|
||||
})
|
||||
return true
|
||||
@@ -359,25 +624,27 @@ func sendReturnMyFarm():
|
||||
return true
|
||||
|
||||
#发送浇水作物信息
|
||||
func sendWaterCrop(lot_index):
|
||||
func sendWaterCrop(lot_index, target_username = ""):
|
||||
if not client.is_client_connected():
|
||||
return false
|
||||
|
||||
client.send_data({
|
||||
"type": "water_crop",
|
||||
"lot_index": lot_index,
|
||||
"target_username": target_username,
|
||||
"timestamp": Time.get_unix_time_from_system()
|
||||
})
|
||||
return true
|
||||
|
||||
#发送施肥作物信息
|
||||
func sendFertilizeCrop(lot_index):
|
||||
func sendFertilizeCrop(lot_index, target_username = ""):
|
||||
if not client.is_client_connected():
|
||||
return false
|
||||
|
||||
client.send_data({
|
||||
"type": "fertilize_crop",
|
||||
"lot_index": lot_index,
|
||||
"target_username": target_username,
|
||||
"timestamp": Time.get_unix_time_from_system()
|
||||
})
|
||||
return true
|
||||
@@ -394,23 +661,137 @@ func sendUpgradeLand(lot_index):
|
||||
})
|
||||
return true
|
||||
|
||||
#发送购买新地块请求
|
||||
func sendBuyNewGround():
|
||||
if not client.is_client_connected():
|
||||
return false
|
||||
|
||||
client.send_data({
|
||||
"type": "buy_new_ground",
|
||||
"timestamp": Time.get_unix_time_from_system()
|
||||
})
|
||||
return true
|
||||
|
||||
#发送点赞玩家请求
|
||||
func sendLikePlayer(target_username):
|
||||
if not client.is_client_connected():
|
||||
return false
|
||||
|
||||
client.send_data({
|
||||
"type": "like_player",
|
||||
"target_username": target_username,
|
||||
"timestamp": Time.get_unix_time_from_system()
|
||||
})
|
||||
return true
|
||||
|
||||
#发送获取在线人数请求
|
||||
func sendGetOnlinePlayers():
|
||||
if not client.is_client_connected():
|
||||
return false
|
||||
|
||||
client.send_data({
|
||||
"type": "request_online_players",
|
||||
"timestamp": Time.get_unix_time_from_system()
|
||||
})
|
||||
return true
|
||||
|
||||
#发送每日签到请求
|
||||
func sendDailyCheckIn():
|
||||
if not client.is_client_connected():
|
||||
return false
|
||||
|
||||
client.send_data({
|
||||
"type": "daily_check_in",
|
||||
"timestamp": Time.get_unix_time_from_system()
|
||||
})
|
||||
return true
|
||||
|
||||
#发送获取签到数据请求
|
||||
func sendGetCheckInData():
|
||||
if not client.is_client_connected():
|
||||
return false
|
||||
|
||||
client.send_data({
|
||||
"type": "get_check_in_data",
|
||||
"timestamp": Time.get_unix_time_from_system()
|
||||
})
|
||||
return true
|
||||
|
||||
#发送幸运抽奖请求
|
||||
func sendLuckyDraw(draw_type: String):
|
||||
if not client.is_client_connected():
|
||||
return false
|
||||
|
||||
client.send_data({
|
||||
"type": "lucky_draw",
|
||||
"draw_type": draw_type, # "single", "five", "ten"
|
||||
"timestamp": Time.get_unix_time_from_system()
|
||||
})
|
||||
return true
|
||||
|
||||
#发送新手大礼包请求
|
||||
func sendClaimNewPlayerGift():
|
||||
if not client.is_client_connected():
|
||||
return false
|
||||
|
||||
client.send_data({
|
||||
"type": "claim_new_player_gift",
|
||||
"timestamp": Time.get_unix_time_from_system()
|
||||
})
|
||||
return true
|
||||
|
||||
#检查是否连接到服务器
|
||||
func is_connected_to_server():
|
||||
return client.is_client_connected()
|
||||
|
||||
# 尝试连接下一个服务器
|
||||
func try_next_server():
|
||||
var original_index = current_server_index
|
||||
current_server_index = (current_server_index + 1) % server_configs.size()
|
||||
|
||||
# 如果回到了原始服务器,说明所有服务器都尝试过了
|
||||
if current_server_index == original_index:
|
||||
if has_tried_all_servers:
|
||||
print("=== 所有服务器连接失败 ===")
|
||||
print("已尝试所有 ", server_configs.size(), " 个服务器")
|
||||
for i in range(server_configs.size()):
|
||||
print("- ", server_configs[i]["name"], " (", server_configs[i]["host"], ":", server_configs[i]["port"], ")")
|
||||
status_label.text = "所有服务器连接失败"
|
||||
status_label.modulate = Color.RED
|
||||
is_trying_to_connect = false
|
||||
return
|
||||
else:
|
||||
has_tried_all_servers = true
|
||||
|
||||
var config = server_configs[current_server_index]
|
||||
|
||||
status_label.text = "尝试连接 " + config["name"]
|
||||
print("尝试连接服务器: ", config["name"], " (", config["host"], ":", config["port"], ")")
|
||||
print("=== 切换到下一个服务器 ===")
|
||||
print("从 ", server_configs[original_index]["name"], " 切换到 ", config["name"])
|
||||
print("新服务器地址: ", config["host"], ":", config["port"])
|
||||
|
||||
var timer = get_tree().create_timer(retry_delay)
|
||||
var timer = get_tree().create_timer(1.0) # 稍微缩短等待时间
|
||||
await timer.timeout
|
||||
|
||||
if not client.is_client_connected():
|
||||
client.connect_to_server(config["host"], config["port"])
|
||||
connect_to_current_server()
|
||||
|
||||
# 手动切换到指定服务器
|
||||
func switch_to_server(server_index: int):
|
||||
if server_index >= 0 and server_index < server_configs.size():
|
||||
current_server_index = server_index
|
||||
has_tried_all_servers = false
|
||||
|
||||
if client.is_client_connected():
|
||||
client.disconnect_from_server()
|
||||
|
||||
# 等待一下再连接新服务器
|
||||
var timer = get_tree().create_timer(0.5)
|
||||
await timer.timeout
|
||||
connect_to_current_server()
|
||||
|
||||
# 获取当前服务器信息
|
||||
func get_current_server_info() -> Dictionary:
|
||||
return server_configs[current_server_index]
|
||||
|
||||
# 检查网络连接状态
|
||||
func check_network_status():
|
||||
@@ -421,4 +802,54 @@ func check_network_status():
|
||||
|
||||
# 尝试连接到当前配置的服务器
|
||||
if not client.is_client_connected():
|
||||
_on_connection_button_pressed()
|
||||
connect_to_current_server()
|
||||
|
||||
# 发送ping消息测量延迟
|
||||
func send_ping():
|
||||
if client.is_client_connected() and not is_measuring_ping:
|
||||
is_measuring_ping = true
|
||||
ping_start_time = Time.get_unix_time_from_system()
|
||||
|
||||
client.send_data({
|
||||
"type": "ping",
|
||||
"timestamp": ping_start_time
|
||||
})
|
||||
|
||||
# 处理服务器返回的pong消息
|
||||
func handle_pong_response(data = null):
|
||||
if is_measuring_ping:
|
||||
var current_time = Time.get_unix_time_from_system()
|
||||
current_ping = int((current_time - ping_start_time) * 1000) # 转换为毫秒
|
||||
is_measuring_ping = false
|
||||
print("延迟: ", current_ping, "ms")
|
||||
|
||||
# 更新连接状态显示
|
||||
update_connection_status()
|
||||
|
||||
# 更新连接状态显示
|
||||
func update_connection_status():
|
||||
if client.is_client_connected():
|
||||
if current_ping >= 0 and not is_measuring_ping:
|
||||
# 根据延迟设置颜色和显示文本
|
||||
var ping_text = str(current_ping) + "ms"
|
||||
var server_name = server_configs[current_server_index]["name"]
|
||||
|
||||
if current_ping < 30:
|
||||
status_label.text = "✅ " + server_name + " " + ping_text
|
||||
status_label.modulate = Color.GREEN
|
||||
elif current_ping < 80:
|
||||
status_label.text = "🟡 " + server_name + " " + ping_text
|
||||
status_label.modulate = Color.YELLOW
|
||||
elif current_ping < 150:
|
||||
status_label.text = "🟠 " + server_name + " " + ping_text
|
||||
status_label.modulate = Color.ORANGE
|
||||
else:
|
||||
status_label.text = "🔴 " + server_name + " " + ping_text
|
||||
status_label.modulate = Color.RED
|
||||
else:
|
||||
var server_name = server_configs[current_server_index]["name"]
|
||||
status_label.text = "🔄 " + server_name + " 测量中..."
|
||||
status_label.modulate = Color.CYAN
|
||||
else:
|
||||
status_label.text = "❌ 未连接"
|
||||
status_label.modulate = Color.RED
|
||||
|
||||
Reference in New Issue
Block a user