Files
Gen_Hack-and-Slash-Roguelite/Client/Assets/Scripts/Network/GrpcClient.cs

38 lines
1.1 KiB
C#

using Grpc.Net.Client;
using Grpc.Net.Client.Web;
using Protocol;
using System.Net.Http;
using System.Threading.Tasks;
using UnityEngine;
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;
public GrpcClient()
{
var channelOptions = new GrpcChannelOptions
{
HttpHandler = new GrpcWebHandler(GrpcWebMode.GrpcWeb, new HttpClientHandler())
};
_channel = GrpcChannel.ForAddress(ServerAddress, channelOptions);
_game = new GameService.GameServiceClient(_channel);
Application.quitting += () => _channel.ShutdownAsync().Wait();
}
public async Task<LoginResponse> Login(string username, string password)
{
return await _game.LoginAsync(new LoginRequest { Username = username, Password = password });
}
}
}