Add gRPC client module and its corresponding test case
This commit is contained in:
@ -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}
|
||||
|
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
|
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
|
Reference in New Issue
Block a user