// 发送消息 public void sendMessage(object msg,int messageID) { byte[] data; using (MemoryStream ms = new MemoryStream()) { ProtoModelSerializer serializer = new ProtoModelSerializer(); serializer.Serialize(ms, msg); data = new byte[ms.Length]; ms.Position = 0; ms.Read(data, 0, data.Length); } VitByteArray arr = new VitByteArray(); arr.WriteInt(data.Length +4); arr.WriteInt(messageID); if (data != null) { arr.WriteBytes(data); } try { socket.Send(arr.Buffer); } catch (SocketException e) { Debug.Log(" " + e.ErrorCode + " " + e.Message); } }
// POST请求 - protobuf IEnumerator POST(string url, object msg, int messageID) { Log.DebugInfo ("发送请求................... messageID == " + messageID); byte[] data; using (MemoryStream ms = new MemoryStream()) { ProtoModelSerializer serializer = new ProtoModelSerializer(); serializer.Serialize(ms, msg); Log.DebugInfo ("serializer..."); data = new byte[ms.Length]; ms.Position = 0; ms.Read(data, 0, data.Length); } System.Text.UTF8Encoding converter = new System.Text.UTF8Encoding(); // string token = "555566"; byte[] tokeyarray = converter.GetBytes(Utils.m_instance.m_useerTokey); VitByteArray arr = new VitByteArray(); arr.WriteInt(messageID); arr.WriteShort (tokeyarray.Length);//token arr.WriteBytes (tokeyarray); // arr.WriteInt(data.Length); if (data != null) { arr.WriteBytes(data); } // Debug.Log ("send...." + url + " " + arr.Length + " " + data.Length + " tokeyarray " + tokeyarray.Length); WWW www = new WWW(url, arr.Buffer); yield return www; if (www.error != null) { //POST请求失败 Log.DebugInfo("error is :" + www.error); } else { parseMessage(www.bytes); } }