extends Node class_name WebSocketClient # WebSocket客户端类 # 导出属性 @export var handshake_headers: PackedStringArray # 握手头部信息 @export var supported_protocols: PackedStringArray # 支持的协议列表 var tls_options: TLSOptions = null # TLS选项,用于加密通信 # WebSocket连接实例和上一个状态 var socket = WebSocketPeer.new() var last_state = WebSocketPeer.STATE_CLOSED # 默认URL #var default_url = "ws://192.168.4.88:9091"#本地 var default_url = "ws://100.86.230.57:9090"#工作站 #var default_url = "ws://100.118.150.125:9090"#真机 # 信号 signal connected_to_server() # 连接成功信号 signal connection_closed() # 连接关闭信号 signal message_received(message: Variant) # 收到消息信号 signal ret_msg(data) func _ready(): link() pass func link(): # 默认连接到 default_url socket.connect_to_url(default_url) # 连接信号到处理函数 socket.connect("message_received", Callable(self, "_on_message_received")) socket.connect("connected_to_server", Callable(self, "_on_connected_to_server")) socket.connect("connection_closed", Callable(self, "_on_closed")) socket.outbound_buffer_size = 655350000 socket.max_queued_packets = 20480000 socket.inbound_buffer_size = 655350000 set_process(true) # 连接到指定的WebSocket URL func connect_to_url(url) -> int: socket.supported_protocols = supported_protocols socket.handshake_headers = handshake_headers var err = socket.connect_to_url(url, tls_options) if err != OK: return err return OK # 发送消息 func send_msg(message) -> int: if typeof(message) == TYPE_STRING: #print("Sending text message:", message) # 打印要发送的文本消息 return socket.send_text(message) # 如果消息是字符串,发送文本消息 else: var message_bytes = var_to_bytes(message) # 将消息转换为字节 #print("Sending binary message:", str(message)) # 打印要发送的二进制消息 return socket.send(message_bytes) # 否则发送二进制消息 # 获取消息并打印到终端 func get_msg() -> Variant: if socket.get_available_packet_count() < 1: return null # 如果没有可用消息,返回null var pkt = socket.get_packet() # 获取消息包 if socket.was_string_packet(): var message = pkt.get_string_from_utf8() # 如果是字符串消息,转换为UTF-8字符串 #print("Received message: ", message) # 打印收到的消息到终端 return message else: var message = bytes_to_var(pkt) # 转换为变量 #print("Received binary message") # 打印收到的二进制消息到终端 return message # 关闭连接 func close(code := 1000, reason := "") -> void: socket.close(code, reason) last_state = socket.get_ready_state() # 清除连接 func clear() -> void: socket = WebSocketPeer.new() last_state = socket.get_ready_state() # 获取WebSocket连接实例 func get_socket() -> WebSocketPeer: return socket # 轮询连接状态并发送/接收消息 func poll() -> void: if socket.get_ready_state() != socket.STATE_CLOSED: socket.poll() var state = socket.get_ready_state() if last_state != state: last_state = state if state == socket.STATE_OPEN: print("已连接到主机.") emit_signal("connected_to_server") elif state == socket.STATE_CLOSED: print("已断开与主机的连接.") emit_signal("connection_closed") elif state == socket.STATE_CONNECTING: print("正在连接到主机.") elif state == socket.STATE_CLOSING: print("使用套接字流关闭.") while socket.get_ready_state() == socket.STATE_OPEN and socket.get_available_packet_count() > 0: var msg_data = get_msg() #ret_msg.emit(msg_data) emit_signal("message_received",str_to_var(msg_data)) # 每帧调用poll()函数 func _process(delta): poll() # 处理接收到的消息 func _on_message_received(message: Variant): print("WebSocket message received: %s" % str(message)) # 处理连接成功 func _on_connected_to_server(): print("Connected to WebSocket server") # 处理连接关闭 func _on_closed(): print("WebSocket connection closed") set_process(false) # 停止处理