public static byte[] getBytes(Message message) { using( MemoryStream mem = new MemoryStream(1024)){ BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器 binFormat.Serialize(mem, message); return mem.GetBuffer(); } }
public void SendTextMessage(string text) { Message msg = new Message(); msg.GUID = Guid.NewGuid().ToString(); msg.message = text; byte[] bytes = MessageHelper.getBytes(msg); this.Send(bytes,0,bytes.Length); }