diff --git a/.gitignore b/.gitignore index f867683..d65564e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -*.meta \ No newline at end of file +*.meta +Proto/ \ No newline at end of file diff --git a/Managers/BattleMgr.cs b/Managers/BattleMgr.cs index 5045160..cdc9267 100644 --- a/Managers/BattleMgr.cs +++ b/Managers/BattleMgr.cs @@ -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 { diff --git a/Net/ClientNet.cs b/Net/ClientNet.cs new file mode 100644 index 0000000..23eb87e --- /dev/null +++ b/Net/ClientNet.cs @@ -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 msg) where T : IMessage + { + 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) + { + + } + + } + +}