轻易实现基于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

相关文章
相关标签/搜索