Files
Gen_Hack-and-Slash-Roguelit…/Client/Assets/Scripts/Network/UnityTcpClient.cs

152 lines
4.4 KiB
C#
Raw Normal View History

2025-07-09 18:52:15 +08:00
using System;
using System.Net.Sockets;
using System.Text;
2025-07-10 22:34:23 +08:00
using System.Threading.Tasks;
2025-07-09 18:52:15 +08:00
using UnityEngine;
using Utils;
2025-07-09 18:52:15 +08:00
namespace Network
2025-07-09 18:52:15 +08:00
{
public class UnityTcpClient : Singleton<UnityTcpClient>
2025-07-09 18:52:15 +08:00
{
private TcpClient _tcpClient;
public bool IsConnected => _tcpClient?.Connected ?? false;
/// <summary>
2025-07-09 19:53:59 +08:00
/// 尝试连接到指定的地址和端口
2025-07-09 18:52:15 +08:00
/// </summary>
/// <param name="address">服务器地址</param>
/// <param name="port">端口号</param>
/// <returns>连接是否成功</returns>
2025-07-10 22:34:23 +08:00
public async Task<bool> Connect(string address, int port)
2025-07-09 18:52:15 +08:00
{
if (IsConnected)
{
Debug.LogWarning("Already connected to a server.");
return true;
}
try
{
// 创建一个新的 TcpClient 实例
_tcpClient = new TcpClient();
// 尝试连接到指定的地址和端口
2025-07-10 22:34:23 +08:00
await _tcpClient.ConnectAsync(address, port);
2025-07-09 18:52:15 +08:00
// 如果连接成功
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;
}
/// <summary>
2025-07-09 19:53:59 +08:00
/// 断开与服务器的连接
2025-07-09 18:52:15 +08:00
/// </summary>
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}");
}
}
/// <summary>
2025-07-09 19:53:59 +08:00
/// 发送数据到服务器
2025-07-09 18:52:15 +08:00
/// </summary>
/// <param name="message">要发送的消息</param>
2025-07-10 22:34:23 +08:00
public async Task Send(string message)
2025-07-09 18:52:15 +08:00
{
if (!IsConnected)
{
Debug.LogError("Cannot send data. Not connected to any server.");
return;
}
try
{
// 获取网络流
2025-07-09 19:53:59 +08:00
var stream = _tcpClient.GetStream();
2025-07-09 18:52:15 +08:00
// 将消息转换为字节数组
2025-07-09 19:53:59 +08:00
var data = Encoding.UTF8.GetBytes(message);
2025-07-09 18:52:15 +08:00
// 发送数据
2025-07-10 22:34:23 +08:00
await stream.WriteAsync(data, 0, data.Length);
2025-07-09 18:52:15 +08:00
Debug.Log($"Sent message to server: {message}");
}
catch (Exception ex)
{
Debug.LogError($"Error while sending: {ex.Message}");
}
}
/// <summary>
2025-07-09 19:53:59 +08:00
/// 接收来自服务器的数据
2025-07-09 18:52:15 +08:00
/// </summary>
/// <returns>接收到的消息</returns>
2025-07-10 22:34:23 +08:00
public async Task<string> Receive()
2025-07-09 18:52:15 +08:00
{
if (!IsConnected)
{
Debug.LogError("Cannot receive data. Not connected to any server.");
return null;
}
try
{
// 获取网络流
2025-07-09 19:53:59 +08:00
var stream = _tcpClient.GetStream();
2025-07-09 18:52:15 +08:00
// 检查是否有可用数据
if (stream.DataAvailable)
{
// 读取数据
2025-07-09 19:53:59 +08:00
var buffer = new byte[1024];
2025-07-10 22:34:23 +08:00
var bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
2025-07-09 18:52:15 +08:00
// 将字节数组转换为字符串
2025-07-09 19:53:59 +08:00
var message = Encoding.UTF8.GetString(buffer, 0, bytesRead);
2025-07-09 18:52:15 +08:00
Debug.Log($"Received message from server: {message}");
return message;
}
}
catch (Exception ex)
{
Debug.LogError($"Error while receiving: {ex.Message}");
}
return null;
}
}
}