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