Beetle.NetPackage是一个多台平开源Client TCP通讯组件,它针对不同平台提供统一的消息描述规则和使用规范可以简单实现多平台下TCP通讯交互.下而介绍通过Beetle.NetPackage简单实地现android和wp聊天.在了解下面的内容之前可以先到Beetle.NetPackage的开源项目了解一下:https://beetlenp.codeplex.com/
作为一个网络通讯程序首要是制定一个协议明确有那些消息交互,在Beetle.NetPackage中所有协议描述都必须通过对象来描述,并实现一个基础的流读写接口IMessage.聊天需要制定两个消息分别就是登陆和发言.下面分别看一下这两个消息在android和wp下的实现.
- android
public class Login implements IMessage { public String Name; @Override public void Load(IDataReader stream) throws Exception { // TODO Auto-generated method stub Name = stream.ReadUTF(); } @Override public void Save(IDataWriter stream) throws Exception { // TODO Auto-generated method stub stream.WriteUTF(Name); } } public class Talk implements IMessage { public String Name; public String Agent; public String Content; @Override public void Load(IDataReader stream) throws Exception { // TODO Auto-generated method stub Name = stream.ReadUTF(); Agent = stream.ReadUTF(); Content = stream.ReadUTF(); } @Override public void Save(IDataWriter stream) throws Exception { // TODO Auto-generated method stub stream.WriteUTF(Name); stream.WriteUTF(Agent); stream.WriteUTF(Content); } }
- WP8
public class Login:Beetle.NetPackage.IMessage { public string Name; public void Load(Beetle.NetPackage.IDataReader reader) { Name = reader.ReadUTF(); } public void Save(Beetle.NetPackage.IDataWriter writer) { writer.WriteUTF(Name); } } public class Talk:Beetle.NetPackage.IMessage { public string Name; public string Content; public string Agent; public void Load(Beetle.NetPackage.IDataReader reader) { Name = reader.ReadUTF(); Agent = reader.ReadUTF(); Content = reader.ReadUTF(); } public void Save(Beetle.NetPackage.IDataWriter writer) { writer.WriteUTF(Name); writer.WriteUTF(Agent); writer.WriteUTF(Content); } }
消息描述完成后就针对协议解释对象实现相应的Package
- android
public class ChatPackage extends Package { @Override protected void WriteMessageType(IDataWriter writer, IMessage message) throws Exception { // TODO Auto-generated method stub writer.WriteUTF(message.getClass().getSimpleName()); } @Override protected IMessage GetMessage(IDataReader reader) throws Exception { // TODO Auto-generated method stub String name= reader.ReadUTF(); if(name.equals("Talk")) { return new Talk(); } else if(name.equals("Login")) { return new Login(); } return null; } }
- wp8
public class ChatPackage:Beetle.NetPackage.Package { protected override void WriteMessageType(Beetle.NetPackage.IDataWriter writer, Beetle.NetPackage.IMessage message) { writer.WriteUTF(message.GetType().Name); } protected override Beetle.NetPackage.IMessage GetMessage(Beetle.NetPackage.IDataReader reader) { string name = reader.ReadUTF(); switch (name) { case "Talk": return new Talk(); case "Login": return new Login(); } return null; } }
定义了消息和协议解释对象后就可以通过NetClient创建服务端的连接,由于Beetle.NetPackage只是Client端的实现服务就根据自己的需要来编写了,这里方便演示就使用了Beetle作为服务端来进行测试.
- android
mClient = new NetClient(getResources().getString(R.string.server_host), 9088, new ChatPackage(), this);
- wp8
mClient = new Beetle.NetPackage.NetClient("192.168.0.104", 9088, new ChatPackage(), this); mClient.LittleEndian = false;
两者定义基于一致,但wp8下NetClient有个LittleEndian属性需要设置,主要原因是android是java数值存储是Big-Endian;而.net默认是Little-Endian因此对应属性要设置False.在编写服务端的时候也应该注意到这一点.以上通讯功能就基础完成,对于数据发送和接收紧紧是消息对象的定义和调用就不多说,详情可以下载源码来了解一下.
最终看下例子运行的效果:
下载完整代码:NPChat.rar (2.68 mb)