private void Send(Hashtable postData) { byte[] byteArray = SerializeUtil.Json2ByteArray(SerializeUtil.Object2Json(postData)); ByteArray sendMsg = new ByteArray(); // 写入长度 sendMsg.Write((short)byteArray.Length); // 写入消息体 sendMsg.Write(byteArray); Debug.Log("Send Text = " + SerializeUtil.ByteArray2String(sendMsg.GetBuff(), MSG_HEAD_LENGTH)); try { _socket.Send(sendMsg.GetBuff()); } catch (Exception e) { Debug.Log("network error, " + e.Message); Reconnect(RECONNECT_DELAY_TIME); } sendMsg.Close(); }
// 缓存中有数据处理 private void OnRecvDataHandler() { // 长度解码 byte[] result = Decode(ref _dataCache); // 长度解码返回空, 说明消息体不全, 等待下条消息过来补全 if (result == null) { _isReading = false; return; } else { Debug.Log("recv from server: " + SerializeUtil.ByteArray2String(result)); Util.ExecutePrompt(delegate(){ MessageManager.Instance.DataParser(SerializeUtil.ByteArray2String(result)); }); } // 尾递归, 防止在消息处理过程中, 有其他消息到达而没有经过处理 OnRecvDataHandler(); }