合并主场景和个人小屋

This commit is contained in:
王浩
2026-02-07 14:11:00 +08:00
parent 603e7d9fc6
commit 326ab7ce5c
360 changed files with 4913 additions and 21701 deletions

View File

@@ -21,7 +21,7 @@ extends Node
# - 通过信号通知连接状态变化
# ============================================================================
class_name WebSocketManager
class_name ChatWebSocketManager
# ============================================================================
# 信号定义
@@ -118,8 +118,6 @@ const HEARTBEAT_INTERVAL: float = 30.0
# 初始化
func _ready() -> void:
print("WebSocketManager 初始化完成")
# 设置重连定时器
_setup_reconnect_timer()
@@ -135,11 +133,6 @@ func _process(_delta: float) -> void:
_check_websocket_state()
var state: WebSocketPeer.State = _websocket_peer.get_ready_state()
# 调试:打印状态变化
if _connection_state == ConnectionState.CONNECTING:
var peer_state_name = ["DISCONNECTED", "CONNECTING", "OPEN", "CLOSING", "CLOSED"][state]
print("📡 WebSocket 状态: peer=%s, manager=%s" % [peer_state_name, ConnectionState.keys()[_connection_state]])
if state == WebSocketPeer.STATE_OPEN:
# 接收数据
@@ -153,9 +146,6 @@ func _process(_delta: float) -> void:
# 发射消息接收信号
data_received.emit(message)
# 打印调试信息
print("📨 WebSocket 收到消息: ", message)
# 清理
func _exit_tree() -> void:
_disconnect()
@@ -178,17 +168,13 @@ func connect_to_game_server() -> void:
push_warning("已经在连接或已连接状态")
return
print("=== WebSocketManager 开始连接 ===")
print("服务器 URL: ", WEBSOCKET_URL)
print("WebSocket 连接中...")
_set_connection_state(ConnectionState.CONNECTING)
_clean_close = true
_reconnect_attempt = 0
var err: Error = _websocket_peer.connect_to_url(WEBSOCKET_URL)
if err != OK:
print("WebSocket 连接失败: ", error_string(err))
push_error("WebSocketManager: 连接失败 - %s" % error_string(err))
_set_connection_state(ConnectionState.ERROR)
return
@@ -197,7 +183,6 @@ func connect_to_game_server() -> void:
# 断开 WebSocket 连接
func disconnect_websocket() -> void:
print("=== WebSocketManager 断开连接 ===")
_disconnect()
# 断开连接(内部方法)
@@ -243,15 +228,14 @@ func get_connection_state() -> ConnectionState:
# Error - 错误码OK 表示成功
func send_message(message: String) -> Error:
if _websocket_peer.get_ready_state() != WebSocketPeer.STATE_OPEN:
print("WebSocket 未连接,无法发送消息")
push_warning("WebSocketManager: 未连接,无法发送消息")
return ERR_UNCONFIGURED
var err: Error = _websocket_peer.send_text(message)
if err != OK:
print("WebSocket 发送消息失败: ", error_string(err))
push_error("WebSocketManager: 发送消息失败 - %s" % error_string(err))
return err
print("📤 发送 WebSocket 消息: ", message)
return OK
# ============================================================================
@@ -272,10 +256,6 @@ func enable_auto_reconnect(enabled: bool, max_attempts: int = DEFAULT_MAX_RECONN
_max_reconnect_attempts = max_attempts
_reconnect_base_delay = base_delay
print("自动重连: ", "启用" if enabled else "禁用")
print("最大重连次数: ", _max_reconnect_attempts)
print("基础重连延迟: ", _reconnect_base_delay, "")
# 获取重连信息
#
# 返回值:
@@ -298,7 +278,6 @@ func _set_connection_state(new_state: ConnectionState) -> void:
return
_connection_state = new_state
print("📡 连接状态变更: ", ConnectionState.keys()[new_state])
# 发射信号
connection_state_changed.emit(new_state)
@@ -332,27 +311,19 @@ func _check_websocket_state() -> void:
WebSocketPeer.STATE_CLOSED:
# 连接关闭
var code: int = _websocket_peer.get_close_code()
var reason: String = _websocket_peer.get_close_reason()
print("🔌 WebSocket 关闭: code=%d, reason=%s" % [code, reason])
_on_websocket_closed(code != 0) # code=0 表示正常关闭
# WebSocket 连接成功处理
func _on_websocket_connected() -> void:
print("✅ WebSocketManager: WebSocket 连接成功")
# 如果是重连,发射重连成功信号
if _connection_state == ConnectionState.RECONNECTING:
_reconnect_attempt = 0
reconnection_succeeded.emit()
print("🔄 重连成功")
_set_connection_state(ConnectionState.CONNECTED)
# WebSocket 连接关闭处理
func _on_websocket_closed(clean_close: bool) -> void:
print("🔌 WebSocketManager: WebSocket 连接断开")
print(" 正常关闭: ", clean_close)
_clean_close = clean_close
# 如果是异常断开且启用了自动重连
@@ -379,7 +350,7 @@ func _setup_reconnect_timer() -> void:
func _attempt_reconnect() -> void:
# 检查是否超过最大重连次数
if _reconnect_attempt >= _max_reconnect_attempts:
print("❌ 达到最大重连次数 (", _max_reconnect_attempts, "),停止重连")
push_error("WebSocketManager: 达到最大重连次数 (%d),停止重连" % _max_reconnect_attempts)
reconnection_failed.emit(_reconnect_attempt, _max_reconnect_attempts)
_set_connection_state(ConnectionState.ERROR)
return
@@ -389,8 +360,6 @@ func _attempt_reconnect() -> void:
# 计算重连延迟(指数退避)
var delay: float = _calculate_reconnect_delay()
print("🔄 尝试重连 (", _reconnect_attempt, "/", _max_reconnect_attempts, ")")
print(" 延迟: ", delay, "")
# 启动重连定时器
_reconnect_timer.start(delay)
@@ -405,7 +374,6 @@ func _calculate_reconnect_delay() -> float:
# 重连定时器超时处理
func _on_reconnect_timeout() -> void:
print("⏰ 重连定时器超时,开始重连...")
_clean_close = false
connect_to_game_server()