fix:修复重连状态与认证流程关键缺陷

This commit is contained in:
2026-03-10 16:17:10 +08:00
parent 88a8eaad02
commit d96abbb8b9
6 changed files with 133 additions and 42 deletions

View File

@@ -106,6 +106,9 @@ var _reconnect_timer: Timer = Timer.new()
# 是否为正常关闭(非异常断开)
var _clean_close: bool = true
# 当前 CLOSED 状态是否已经处理过(防止每帧重复处理 close 事件)
var _closed_state_handled: bool = false
# 心跳定时器
var _heartbeat_timer: Timer = Timer.new()
@@ -163,14 +166,20 @@ func _exit_tree() -> void:
# ============================================================================
# 连接到游戏服务器
func connect_to_game_server() -> void:
func connect_to_game_server(is_reconnect_attempt: bool = false) -> void:
if _connection_state == ConnectionState.CONNECTED or _connection_state == ConnectionState.CONNECTING:
push_warning("已经在连接或已连接状态")
return
_set_connection_state(ConnectionState.CONNECTING)
if is_reconnect_attempt:
_set_connection_state(ConnectionState.RECONNECTING)
else:
_set_connection_state(ConnectionState.CONNECTING)
_clean_close = true
_reconnect_attempt = 0
_closed_state_handled = false
# 仅在首次/手动连接时重置重连计数,重连流程保持累计尝试次数
if not is_reconnect_attempt:
_reconnect_attempt = 0
var err: Error = _websocket_peer.connect_to_url(WEBSOCKET_URL)
if err != OK:
@@ -295,23 +304,40 @@ func _check_websocket_state() -> void:
match state:
WebSocketPeer.STATE_CONNECTING:
_closed_state_handled = false
# 正在连接
if _connection_state != ConnectionState.CONNECTING and _connection_state != ConnectionState.RECONNECTING:
_set_connection_state(ConnectionState.CONNECTING)
WebSocketPeer.STATE_OPEN:
_closed_state_handled = false
# 连接成功
if _connection_state != ConnectionState.CONNECTED:
_on_websocket_connected()
WebSocketPeer.STATE_CLOSING:
_closed_state_handled = false
# 正在关闭
pass
WebSocketPeer.STATE_CLOSED:
# 连接关闭
var code: int = _websocket_peer.get_close_code()
_on_websocket_closed(code != 0) # code=0 表示正常关闭
if _closed_state_handled:
return
_closed_state_handled = true
# 仅在连接生命周期中发生的关闭才触发关闭处理
var should_handle_close: bool = (
_connection_state == ConnectionState.CONNECTED
or _connection_state == ConnectionState.CONNECTING
or _connection_state == ConnectionState.RECONNECTING
)
if should_handle_close:
var close_code: int = _websocket_peer.get_close_code()
_on_websocket_closed(_is_clean_close_code(close_code))
# WebSocket 连接成功处理
func _on_websocket_connected() -> void:
@@ -333,6 +359,11 @@ func _on_websocket_closed(clean_close: bool) -> void:
else:
_set_connection_state(ConnectionState.DISCONNECTED)
# 判断关闭码是否为干净关闭
# Godot 中 close_code == -1 表示非干净关闭(异常断开)
func _is_clean_close_code(close_code: int) -> bool:
return close_code != -1
# ============================================================================
# 内部方法 - 重连机制
# ============================================================================
@@ -375,7 +406,7 @@ func _calculate_reconnect_delay() -> float:
# 重连定时器超时处理
func _on_reconnect_timeout() -> void:
_clean_close = false
connect_to_game_server()
connect_to_game_server(true)
# ============================================================================
# 内部方法 - 心跳机制