輕易實現基於linux或win運行的聊天服務端程序


        對於不了解網絡編程的開發人員來說,編寫一個良好的服務端通訊程序是一件比較麻煩的事情.然而通過EC這個免費組件你可以非常簡單地構建一個基於linux或win部署運行的網絡服務程序.這種便利性完全得益於mono這些年來的不停發展.下面介紹通過EC這個組件如何通過短短十來分鍾的時候內就能實現一個聊天室通訊服務程序.

        在實現一個網絡通訊程序的時候需要定義一個通訊協議,但EC已經集成了基礎的協議功能,只需要根據交互的數據定義消息類型即可(EC提供兩種序列化對象描述分別是protobuf和msgpack).

消息定義

            針對簡單的聊到室只需要定義登進,登出和發言這幾個消息如下:

    [MessageID(0x0001)]
    [ProtoContract]
    public class Login
    {
        [ProtoMember(1)]
        public string Name { get; set; }
        [ProtoMember(2)]
        public string From { get; set; }
    }
    [MessageID(0x0003)]
    [ProtoContract]
    public class Signout
    {
        [ProtoMember(1)]
        public string Name { get; set; }
        [ProtoMember(2)]
        public string From { get; set; }
    }
    [MessageID(0x0002)]
    [ProtoContract]
    public class Say
    {
        [ProtoMember(1)]
        public string Content { get; set; }
        [ProtoMember(3)]
        public string From { get; set; }
        [ProtoMember(2)]
        public string Name { get; set; }
    }

服務端

        消息定義完成那用EC來制定一個聊天轉發的服務端來說則是件非常簡單的事情,只需要十來行代碼就可以構建聊天和服務啟動等相關功能.

    [EC.Controller]
    public class Program
    {
        static void Main(string[] args)
        {
            EC.ECServer.Open();
            System.Threading.Thread.Sleep(-1);
        }
        public void OnLogin(EC.ISession session, Chat.Login e)
        {
            session.Channel.Name = e.Name;
            e.From = session.Channel.EndPoint.ToString();
            foreach (Beetle.Express.IChannel other in session.Application.Server.GetOnlines())
            {
                if (other != session.Channel)
                    session.Application.Server.Send(e, other);
            }
        }
        public void OnSay(EC.ISession session, Chat.Say e)
        {
            e.Name = session.Channel.Name;
            e.From = session.Channel.EndPoint.ToString();
            foreach (Beetle.Express.IChannel other in session.Application.Server.GetOnlines())
            {
                if (other != session.Channel)
                    session.Application.Server.Send(e, other);
            }
        }
    }

        以上一個簡單的聊取室的登進和聊天的功能,不過還有一個需要我們去處理的就是當用戶斷開后如果反映給其他用戶.在EC中監控連接斷開的過程需要通過一個AppModel來監控,發布有連接斷開了則向其他連接發送登出信息,代碼如下:

    public class AppModel : EC.IAppModel
    {
        public void Init(EC.IApplication application)
        {
            application.Disconnected += (o, e) =>
            {
                Beetle.Express.IChannel channel = e.Session.Channel;
                Chat.Signout msg = new Signout();
                msg.Name = channel.Name;
                msg.From = channel.EndPoint.ToString();
                foreach (Beetle.Express.IChannel other in application.Server.GetOnlines())
                {
                    if (other != channel)
                        application.Server.Send(msg, other);
                }
            };
        }

        public string Name
        {
            get { return "AppModel"; }
        }

        public string Command(string cmd)
        {
            throw new NotImplementedException();
        }
    }

        EC提供一個IAppModel的自定義功能,通過AppModel可以監控用戶會話,和處理全局消息的能力;在以后的文章再詳細介紹.

客戶端

        EC同樣提供便利的Client功能對象,你只需要定義簡單的代碼就可以向對應的服務端發送和接收相應的消息來處理.

EC.ProtoClient mClient = new EC.ProtoClient("127.0.0.1");
 mClient.Receive = (o, p) => {
                if (p.Message is Say)
                {
                    Invoke(new Action<Say>(OnSay), p.Message);
                }
                else if (p.Message is Login)
                {
                    Invoke(new Action<Login>(OnLogin), p.Message);
                }
                else if (p.Message is Signout)
                {
                    Invoke(new Action<Signout>(OnSignout), p.Message);
                }
            };
mClient.Send(new Say{ Content=t"你好"});

        借助於Xamarin我們還可以同樣的方式把功能移植到不同平台下運行如android,ios等

		private IServiceChannel mClient = new ServiceChannel("10.0.2.2",10034);
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			ServiceChannel.Register (typeof(MainActivity).Assembly);
			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);
			EditText name = FindViewById<EditText> (Resource.Id.txtname);
			EditText say = FindViewById<EditText> (Resource.Id.txtsay);
			TextView content = FindViewById<TextView> (Resource.Id.txtContent);
			mClient.Receive = (o, p) => {
				content.Post(delegate {
					content.Append(p.Message.ToString());
				});
			};

			FindViewById<Button> (Resource.Id.btnlogin).Click += delegate {
				Login login = new Login();
				login.Name = name.Text;
				mClient.Send(login);
			};
			FindViewById<Button> (Resource.Id.btnsay).Click += delegate {
				Say s = new Say{ Content=say.Text};
				mClient.Send(s);

			};
			// Get our button from the layout resource,
			// and attach an event to it	
		}

        這樣一個多平台的基礎聊天功能就完成了

    

    示例代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM