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) { } } }