From 3f38673054d4042ef1d5435cdacfc30e0177d6a9 Mon Sep 17 00:00:00 2001 From: m0_75251201 Date: Wed, 9 Jul 2025 18:52:15 +0800 Subject: [PATCH] =?UTF-8?q?(client)=20feat:=E6=B7=BB=E5=8A=A0=E7=BD=91?= =?UTF-8?q?=E7=BB=9C=E7=B1=BBNetwork?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Client/.vsconfig | 6 + Client/Assets/Scripts/Utils/Network.cs | 150 ++++++++++++++++++++ Client/Assets/Scripts/Utils/Network.cs.meta | 2 + 3 files changed, 158 insertions(+) create mode 100644 Client/.vsconfig create mode 100644 Client/Assets/Scripts/Utils/Network.cs create mode 100644 Client/Assets/Scripts/Utils/Network.cs.meta diff --git a/Client/.vsconfig b/Client/.vsconfig new file mode 100644 index 0000000..f019fd0 --- /dev/null +++ b/Client/.vsconfig @@ -0,0 +1,6 @@ +{ + "version": "1.0", + "components": [ + "Microsoft.VisualStudio.Workload.ManagedGame" + ] +} diff --git a/Client/Assets/Scripts/Utils/Network.cs b/Client/Assets/Scripts/Utils/Network.cs new file mode 100644 index 0000000..16d5cd9 --- /dev/null +++ b/Client/Assets/Scripts/Utils/Network.cs @@ -0,0 +1,150 @@ +using System; +using System.Net.Sockets; +using System.Text; +using UnityEngine; + +namespace Utils +{ + public class Network : Singleton + { + private TcpClient _tcpClient; + public bool IsConnected => _tcpClient?.Connected ?? false; + + /// + /// 尝试连接到指定的地址和端口 + /// + /// 服务器地址 + /// 端口号 + /// 连接是否成功 + public bool Connect(string address, int port) + { + if (IsConnected) + { + Debug.LogWarning("Already connected to a server."); + return true; + } + + try + { + // 创建一个新的 TcpClient 实例 + _tcpClient = new TcpClient(); + + // 尝试连接到指定的地址和端口 + _tcpClient.Connect(address, port); + + // 如果连接成功 + if (IsConnected) + { + Debug.Log($"Successfully connected to server at {address}:{port}"); + return true; + } + } + catch (SocketException ex) + { + Debug.LogError($"Failed to connect to server at {address}:{port}. Error: {ex.Message}"); + } + catch (Exception ex) + { + Debug.LogError($"An unexpected error occurred: {ex.Message}"); + } + + // 如果发生任何错误或连接失败 + Disconnect(); + return false; + } + + /// + /// 断开与服务器的连接 + /// + public void Disconnect() + { + if (!IsConnected) + { + Debug.LogWarning("Not currently connected to any server."); + return; + } + + try + { + // 关闭 TcpClient 连接 + _tcpClient?.Close(); + + Debug.Log("Disconnected from the server."); + } + catch (Exception ex) + { + Debug.LogError($"Error while disconnecting: {ex.Message}"); + } + } + + /// + /// 发送数据到服务器 + /// + /// 要发送的消息 + public void Send(string message) + { + if (!IsConnected) + { + Debug.LogError("Cannot send data. Not connected to any server."); + return; + } + + try + { + // 获取网络流 + NetworkStream stream = _tcpClient.GetStream(); + + // 将消息转换为字节数组 + byte[] data = Encoding.UTF8.GetBytes(message); + + // 发送数据 + stream.Write(data, 0, data.Length); + + Debug.Log($"Sent message to server: {message}"); + } + catch (Exception ex) + { + Debug.LogError($"Error while sending: {ex.Message}"); + } + } + + /// + /// 接收来自服务器的数据 + /// + /// 接收到的消息 + public string Receive() + { + if (!IsConnected) + { + Debug.LogError("Cannot receive data. Not connected to any server."); + return null; + } + + try + { + // 获取网络流 + NetworkStream stream = _tcpClient.GetStream(); + + // 检查是否有可用数据 + if (stream.DataAvailable) + { + // 读取数据 + byte[] buffer = new byte[1024]; + int bytesRead = stream.Read(buffer, 0, buffer.Length); + + // 将字节数组转换为字符串 + string message = Encoding.UTF8.GetString(buffer, 0, bytesRead); + + Debug.Log($"Received message from server: {message}"); + return message; + } + } + catch (Exception ex) + { + Debug.LogError($"Error while receiving: {ex.Message}"); + } + + return null; + } + } +} \ No newline at end of file diff --git a/Client/Assets/Scripts/Utils/Network.cs.meta b/Client/Assets/Scripts/Utils/Network.cs.meta new file mode 100644 index 0000000..a153c7c --- /dev/null +++ b/Client/Assets/Scripts/Utils/Network.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 3cfed6461b8d6714bbc44fdac509c880 \ No newline at end of file