113 lines
3.7 KiB
C#
113 lines
3.7 KiB
C#
|
using System;
|
|||
|
using System.IO;
|
|||
|
using System.Text;
|
|||
|
using Game;
|
|||
|
using ProtoBuf;
|
|||
|
using ProtoBuf.Meta;
|
|||
|
using ProtoBuf.Serializers;
|
|||
|
using UnityEngine;
|
|||
|
using WebSocketSharp;
|
|||
|
using ZGame;
|
|||
|
using ErrorEventArgs = WebSocketSharp.ErrorEventArgs;
|
|||
|
|
|||
|
namespace ProtoTest
|
|||
|
{
|
|||
|
public class Test : MonoBehaviour
|
|||
|
{
|
|||
|
private HttpManager httpManager;
|
|||
|
WebSocket webSocket;
|
|||
|
|
|||
|
private void Start()
|
|||
|
{
|
|||
|
this.httpManager = new HttpManager();
|
|||
|
webSocket = new WebSocket(NetworkConst.socketUrl);
|
|||
|
webSocket.OnMessage += MM;
|
|||
|
webSocket.OnError += Erroe;
|
|||
|
webSocket.Connect();
|
|||
|
}
|
|||
|
|
|||
|
private void Erroe(object sender, ErrorEventArgs e)
|
|||
|
{
|
|||
|
UnityEngine.Debug.Log("Error " + e.Message);
|
|||
|
}
|
|||
|
|
|||
|
private void OnDestroy()
|
|||
|
{
|
|||
|
this.webSocket.Close();
|
|||
|
}
|
|||
|
|
|||
|
private void Update()
|
|||
|
{
|
|||
|
if (Input.GetKeyDown(KeyCode.A))
|
|||
|
{
|
|||
|
// StartCoroutine(this.httpManager.LoginAsync("123", "123", null));
|
|||
|
var person = new C2SLogin()
|
|||
|
{
|
|||
|
RequestKey = "1111111", Info = "common", LabelName = "login"
|
|||
|
};
|
|||
|
using (var file = File.Create("example.bin"))
|
|||
|
{
|
|||
|
Serializer.Serialize(file, person);
|
|||
|
}
|
|||
|
|
|||
|
string str = "{\n\t\"requestKey\": \"1221321\",\n\t\"info\": \"common\",\n\t\"labelName\": \"login\",\n\t\"token\": \"68c5e428a73b9f4438b80ed28e594675\"\n}";
|
|||
|
webSocket.Send(str); // this.Se()
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void MM(object sender, MessageEventArgs e)
|
|||
|
{
|
|||
|
Debug.Log($"OnMessage {e.Data}");
|
|||
|
// if (e.Data == null || e.Data.Contains("ping")) return;
|
|||
|
this.De(e.RawData);
|
|||
|
}
|
|||
|
|
|||
|
byte[] Se()
|
|||
|
{
|
|||
|
var person = new C2SLogin()
|
|||
|
{
|
|||
|
RequestKey = "1111111", Info = "common", LabelName = "login"
|
|||
|
};
|
|||
|
using MemoryStream stream = new MemoryStream();
|
|||
|
Serializer.Serialize(stream, person);
|
|||
|
var array = stream.ToArray();
|
|||
|
Debug.Log(array);
|
|||
|
return array;
|
|||
|
}
|
|||
|
|
|||
|
void De(byte[] obj)
|
|||
|
{
|
|||
|
// // 序列化 User 对象到字节数组
|
|||
|
// using (var memoryStream = new MemoryStream())
|
|||
|
// {
|
|||
|
// Serializer.Serialize(memoryStream, user);
|
|||
|
// byte[] serializedBytes = memoryStream.ToArray();
|
|||
|
//
|
|||
|
// // 现在 serializedBytes 包含了序列化后的 Protobuf 数据
|
|||
|
//
|
|||
|
// // 如果你需要将这个字节数组写入文件或发送到网络,你可以直接使用 serializedBytes
|
|||
|
//
|
|||
|
// // 示例:写入文件
|
|||
|
// // File.WriteAllBytes("user.bin", serializedBytes);
|
|||
|
//
|
|||
|
// // 反序列化字节数组回到 User 对象
|
|||
|
// var deserializedUser = Serializer.Deserialize<User>(new MemoryStream(serializedBytes));
|
|||
|
//
|
|||
|
// // 输出反序列化后的 User 对象信息
|
|||
|
// Console.WriteLine($"Deserialized User: {deserializedUser.Name}, ID: {deserializedUser.Id}, Email: {deserializedUser.Email}");
|
|||
|
// }
|
|||
|
|
|||
|
// var fromBase64String = Convert.FromBase64String(obj.ToString());
|
|||
|
var s = Encoding.UTF8.GetString(obj);
|
|||
|
Debug.Log(s);
|
|||
|
|
|||
|
//
|
|||
|
// S2CLogin s2CLogin;
|
|||
|
// using MemoryStream stream = new MemoryStream();
|
|||
|
// stream.Write(fromBase64String);
|
|||
|
// stream.Position = 0;
|
|||
|
// s2CLogin = Serializer.Deserialize<S2CLogin>(stream);
|
|||
|
// Debug.Log(s2CLogin.Code + "\n" + s2CLogin.Data);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|