Compare commits
2 Commits
2a5d3b6069
...
37d52aa631
Author | SHA1 | Date | |
---|---|---|---|
37d52aa631 | |||
b3e0ef5104 |
@ -303,50 +303,6 @@ Transform:
|
||||
- {fileID: 9462743}
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!1 &912467177
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 912467178}
|
||||
- component: {fileID: 912467179}
|
||||
m_Layer: 0
|
||||
m_Name: NetworkConnectionTest
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 0
|
||||
--- !u!4 &912467178
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 912467177}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children: []
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &912467179
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 912467177}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 289a7efc822fe7347adfbf218522e8a2, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
--- !u!1 &1057087086
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@ -479,7 +435,6 @@ Transform:
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: -10}
|
||||
m_LocalPosition: {x: 0, y: 0, z: -10}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children: []
|
||||
@ -696,6 +651,50 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: 757576bb4354ac54da09868e1be02eec, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
--- !u!1 &2104915506
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 2104915508}
|
||||
- component: {fileID: 2104915507}
|
||||
m_Layer: 0
|
||||
m_Name: GrpcClientTest
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!114 &2104915507
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2104915506}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 98c1b145899d4d97bd981b177b5c45a9, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
--- !u!4 &2104915508
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2104915506}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: -0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children: []
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!1660057539 &9223372036854775807
|
||||
SceneRoots:
|
||||
m_ObjectHideFlags: 0
|
||||
@ -703,6 +702,5 @@ SceneRoots:
|
||||
- {fileID: 1371068282}
|
||||
- {fileID: 1057087090}
|
||||
- {fileID: 613797070}
|
||||
- {fileID: 912467178}
|
||||
- {fileID: 1485465861}
|
||||
- {fileID: 1485465861}
|
||||
- {fileID: 2104915508}
|
||||
|
@ -1,8 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 36bb272d9a70ff347965919811a887de
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
guid: 178450a3675642d799764a426bcb236f
|
||||
timeCreated: 1752480692
|
48
Client/Assets/Scripts/Network/GrpcClient.cs
Normal file
48
Client/Assets/Scripts/Network/GrpcClient.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Grpc.Net.Client;
|
||||
using Grpc.Net.Client.Web;
|
||||
using Protocol;
|
||||
using Utils;
|
||||
|
||||
namespace Network
|
||||
{
|
||||
public class GrpcClient : Singleton<GrpcClient>
|
||||
{
|
||||
// The address must adopt HTTP.
|
||||
private const string ServerAddress = "http://127.0.0.1:12345";
|
||||
|
||||
private readonly GrpcChannel _channel;
|
||||
private readonly GameService.GameServiceClient _game;
|
||||
private readonly GeneralService.GeneralServiceClient _general;
|
||||
|
||||
public GrpcClient()
|
||||
{
|
||||
var httpHandler = new GrpcWebHandler(GrpcWebMode.GrpcWeb, new HttpClientHandler());
|
||||
var channelOptions = new GrpcChannelOptions
|
||||
{
|
||||
HttpHandler = httpHandler
|
||||
};
|
||||
|
||||
_channel = GrpcChannel.ForAddress(ServerAddress, channelOptions);
|
||||
|
||||
_general = new GeneralService.GeneralServiceClient(_channel);
|
||||
_game = new GameService.GameServiceClient(_channel);
|
||||
}
|
||||
|
||||
~GrpcClient()
|
||||
{
|
||||
_channel.ShutdownAsync().Wait();
|
||||
}
|
||||
|
||||
public async Task<ServerInfo> GetServerInfo()
|
||||
{
|
||||
return await _general.GetServerInfoAsync(new Empty());
|
||||
}
|
||||
|
||||
public async Task<LoginResponse> Login(string username, string password)
|
||||
{
|
||||
return await _game.LoginAsync(new LoginRequest { Username = username, Password = password });
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Network/GrpcClient.cs.meta
Normal file
3
Client/Assets/Scripts/Network/GrpcClient.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bd54dd152e0e4bbda802e9aa04078197
|
||||
timeCreated: 1752480707
|
@ -1,152 +0,0 @@
|
||||
using System;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using Utils;
|
||||
|
||||
namespace Network
|
||||
{
|
||||
public class UnityTcpClient : Singleton<UnityTcpClient>
|
||||
{
|
||||
private TcpClient _tcpClient;
|
||||
public bool IsConnected => _tcpClient?.Connected ?? false;
|
||||
|
||||
/// <summary>
|
||||
/// 尝试连接到指定的地址和端口
|
||||
/// </summary>
|
||||
/// <param name="address">服务器地址</param>
|
||||
/// <param name="port">端口号</param>
|
||||
/// <returns>连接是否成功</returns>
|
||||
public async Task<bool> Connect(string address, int port)
|
||||
{
|
||||
if (IsConnected)
|
||||
{
|
||||
Debug.LogWarning("Already connected to a server.");
|
||||
return true;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 创建一个新的 TcpClient 实例
|
||||
_tcpClient = new TcpClient();
|
||||
|
||||
// 尝试连接到指定的地址和端口
|
||||
await _tcpClient.ConnectAsync(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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 断开与服务器的连接
|
||||
/// </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>
|
||||
/// 发送数据到服务器
|
||||
/// </summary>
|
||||
/// <param name="message">要发送的消息</param>
|
||||
public async Task Send(string message)
|
||||
{
|
||||
if (!IsConnected)
|
||||
{
|
||||
Debug.LogError("Cannot send data. Not connected to any server.");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 获取网络流
|
||||
var stream = _tcpClient.GetStream();
|
||||
|
||||
// 将消息转换为字节数组
|
||||
var data = Encoding.UTF8.GetBytes(message);
|
||||
|
||||
// 发送数据
|
||||
await stream.WriteAsync(data, 0, data.Length);
|
||||
|
||||
Debug.Log($"Sent message to server: {message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"Error while sending: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 接收来自服务器的数据
|
||||
/// </summary>
|
||||
/// <returns>接收到的消息</returns>
|
||||
public async Task<string> Receive()
|
||||
{
|
||||
if (!IsConnected)
|
||||
{
|
||||
Debug.LogError("Cannot receive data. Not connected to any server.");
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 获取网络流
|
||||
var stream = _tcpClient.GetStream();
|
||||
|
||||
// 检查是否有可用数据
|
||||
if (stream.DataAvailable)
|
||||
{
|
||||
// 读取数据
|
||||
var buffer = new byte[1024];
|
||||
var bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
|
||||
|
||||
// 将字节数组转换为字符串
|
||||
var 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3cfed6461b8d6714bbc44fdac509c880
|
19
Client/Assets/Scripts/Test/GrpcClientTest.cs
Normal file
19
Client/Assets/Scripts/Test/GrpcClientTest.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using Network;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Test
|
||||
{
|
||||
public class GrpcClientTest : MonoBehaviour
|
||||
{
|
||||
private async void Start()
|
||||
{
|
||||
var serverInfo = await GrpcClient.Instance.GetServerInfo();
|
||||
|
||||
Debug.Log($"Received info from server: {serverInfo}");
|
||||
|
||||
var loginResult = await GrpcClient.Instance.Login("野兽先辈", "114514");
|
||||
|
||||
Debug.Log($"Received login result: {loginResult.Result}");
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Test/GrpcClientTest.cs.meta
Normal file
3
Client/Assets/Scripts/Test/GrpcClientTest.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 98c1b145899d4d97bd981b177b5c45a9
|
||||
timeCreated: 1752478138
|
@ -1,26 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using Network;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Test
|
||||
{
|
||||
public class NetworkConnectionTest : MonoBehaviour
|
||||
{
|
||||
private async void Start()
|
||||
{
|
||||
await BasicTest();
|
||||
}
|
||||
|
||||
private static async Task BasicTest()
|
||||
{
|
||||
var result = await UnityTcpClient.Instance.Connect("127.0.0.1", 12345);
|
||||
if (result) Debug.Log("Connected to server!");
|
||||
else Debug.LogError("Failed to connect to server!");
|
||||
|
||||
string buffer = null;
|
||||
while (buffer is null) buffer = await UnityTcpClient.Instance.Receive();
|
||||
|
||||
Debug.Log($"Received contents: {buffer}");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 289a7efc822fe7347adfbf218522e8a2
|
Reference in New Issue
Block a user