(tools, client, server) feat: Complete ProtoBuf message transmission with both TCP and UDP

This commit is contained in:
2025-08-30 21:25:17 +08:00
parent 362aa799b9
commit 450b15e4df
13 changed files with 149 additions and 35 deletions

View File

@ -1,5 +1,7 @@
using Google.Protobuf;
using Network;
using System.Text;
using Protocol;
using System.Collections.Generic;
using UnityEngine;
namespace Test
@ -8,12 +10,30 @@ namespace Test
{
private async void Start()
{
var sendBytes = Encoding.UTF8.GetBytes("This is a test string sent via TCP.");
var request = new LoginRequest
{
Username = "原神启动通过TCP",
Password = "20200928",
};
var receivedBytes = await UnityTcpClient.Instance.SendAndReceiveData(sendBytes);
var receivedString = Encoding.UTF8.GetString(receivedBytes);
var requestBytes = new byte[request.CalculateSize()];
request.WriteTo(requestBytes);
Debug.Log($"Received string: {receivedString}");
var sendBytes = new List<byte>
{
(byte)MessageType.LoginRequest
};
sendBytes.AddRange(requestBytes);
var responseBytes = await UnityTcpClient.Instance.SendAndReceiveData(sendBytes.ToArray());
if (responseBytes.Length == 0) return;
else if (responseBytes[0] == (byte)MessageType.LoginResponse)
{
var response = LoginResponse.Parser.ParseFrom(responseBytes[1..]);
Debug.Log($"Received response: {response}");
}
}
}
}