This commit is contained in:
azw 2024-05-18 22:33:25 +08:00
parent 81dfa342a6
commit ee123745b1
3 changed files with 109 additions and 11 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
*.meta
*.meta
Proto/

View File

@ -3,18 +3,8 @@ using LuaFramework;
using BestHTTP;
using BestHTTP.WebSocket;
using System;
using Newtonsoft;
using System.Collections.Generic;
public class Test
{
//[Newtonsoft.Json.JsonConverter(typeof(string))]
public string id;
[Newtonsoft.Json.JsonProperty("Name")]
public string name;
public int age;
}
public class BattleMgr
{

107
Net/ClientNet.cs Normal file
View File

@ -0,0 +1,107 @@
using UnityEngine;
using LuaFramework;
using BestHTTP;
using BestHTTP.WebSocket;
using System;
using System.Collections.Generic;
using Google.Protobuf;
using pbr = global::Google.Protobuf.Reflection;
namespace Net
{
public class ClientNet
{
private string uri;
private WebSocket ws;
public ClientNet(string uri)
{
this.ws = new WebSocket(new Uri(uri));
this.ws.OnOpen += this.OnOpen;
this.ws.OnBinary += this.OnBinaryReceived;
this.ws.OnError += this.OnError;
this.ws.OnClosed += this.OnClosed;
}
public void Open()
{
this.ws.Open();
battle.cs.CMJoin joinMsg = new battle.cs.CMJoin();
this.SendMsg(joinMsg);
battle.cs.CMMove moveMsg = new battle.cs.CMMove();
this.SendMsg(moveMsg);
}
public void SendMsg<T>(T msg) where T : IMessage<T>
{
int msgId = 0;
Type enumType = typeof(battle.cs.CMMessageId_e);
foreach(var f in enumType.GetFields())
{
var tAttrs = f.GetCustomAttributes(typeof(pbr::OriginalNameAttribute), false);
if (tAttrs.Length > 0) {
var tAttr = (pbr::OriginalNameAttribute)tAttrs[0];
if (tAttr.Name.Equals("")) {
}
}
}
/*
var b = by.ToBytes();
by.Close();
ByteBuffer buffer = new ByteBuffer();
buffer.WriteShort((ushort)b.Length);
buffer.WriteShort((ushort)msgid);
buffer.WriteInt(0);
buffer.WriteShort(21323);
buffer.WriteShort(0);
buffer.WriteBytesNo(b);
var buf = buffer.ToBytes();
m_webSocket.Send(buf);
*/
int packLen = msg.CalculateSize();
const int magicCode = 21323;
byte[] data = new byte[12 + packLen];
data[0] = (byte)(packLen & 0xFF);
data[1] = (byte)((packLen & 0xFFFF) >> 8);
data[2] = (byte)(msgId & 0xFF);
data[3] = (byte)((msgId & 0xFFFF) >> 8);
data[4] = 0;
data[5] = 0;
data[6] = 0;
data[7] = 0;
data[8] = (byte)(magicCode & 0xFF);
data[9] = (byte)((magicCode & 0xFFFF) >> 8);
data[10] = 0;
data[11] = 0;
msg.ToByteArray().CopyTo(data, 12);
this.ws.Send(data);
}
private void OnOpen(WebSocket ws)
{
}
private void OnBinaryReceived(WebSocket ws, byte[] data)
{
}
private void OnError(WebSocket ws, string reason)
{
}
private void OnClosed(WebSocket ws, UInt16 code, string message)
{
}
}
}