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

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

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

消息定義

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

[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來制定一個聊天轉發的服務端來講則是件很是簡單的事情,只須要十來行代碼就能夠構建聊天和服務啓動等相關功能. git

[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來監控,發佈有鏈接斷開了則向其餘鏈接發送登出信息,代碼以下: github

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等 session

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	
		}

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

        

示例代碼 ide

我的開源項目github.com/IKende

elastic communication component for .net

相關文章
相關標籤/搜索