使用Beetle.NetPackage簡單實現android和wp聊天

smark發表於2013-08-23

  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)

相關文章