/// <summary> /// 消息体序列化 /// </summary> /// <param name="value"></param> /// <returns></returns> public static byte[] encode(object value) { SocketModel model = value as SocketModel; ByteArray ba = new ByteArray();//工具类,将数据转为二进制数组 ba.write(model.type); ba.write(model.area); ba.write(model.command); if (model.message != null) { ba.write(SerialUtil.encode(model.message)); } byte[] result = ba.getBuff(); ba.Close(); return(result); }
public void write(byte type, int area, int command, object message) { ByteArray ba = new ByteArray(); ba.write(type); ba.write(area); ba.write(command); if (message != null) { ba.write(SerialUtil.encode(message)); } ByteArray ba1 = new ByteArray(); ba1.write(ba.Length); ba1.write(ba.getBuff()); try { socket.Send(ba1.getBuff()); } catch (Exception e) { Debug.Log("网络错误,请重新登陆" + e.Message); } }