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

smark發表於2014-10-13

        對於不瞭解網路程式設計的開發人員來說,編寫一個良好的服務端通訊程式是一件比較麻煩的事情.然而通過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	
		}

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

    

    示例程式碼

相關文章