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 { // 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 Login(string username, string password) { return await _game.LoginAsync(new LoginRequest { Username = username, Password = password }); } } }