Add gRPC client module and its corresponding test case

This commit is contained in:
2025-07-14 16:51:47 +08:00
parent b3e0ef5104
commit 37d52aa631
5 changed files with 118 additions and 47 deletions

View 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 });
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bd54dd152e0e4bbda802e9aa04078197
timeCreated: 1752480707