Easy to implement the chat server program based on Linux or win run

Source: Internet
Author: User

For developers who do not understand network programming, Writing a good service-side communication program is a hassle. However, with the EC this free component you can easily build a network service program that runs on a Linux or win-only deployment. This convenience is fully benefited from the continuous development of mono over the years. Here's how this component through the EC passes in just 10 minutes Can implement a chat room Communication service program.

When implementing a network communication program, you need to define a communication protocol, but the EC has integrated the underlying protocol functionality by defining the message type based on the interactive data (EC provides two serialization object descriptions, Protobuf and Msgpack respectively).

Message definition

For simple chat-to-room only need to define login, logout and speak these few messages are as follows:

[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;}    }
Service side

Message definition It is very simple to use EC to set up a service side of chat forwarding, and it takes only a dozen lines of code to build chat and service startup related functions.

[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); }        }    }

        above a simple chat room login and chat function, but there is a need for us to deal with when the user is disconnected if reflected to other users. Monitoring the disconnection process in the EC requires a Appmodel to monitor the connection, and then sends the logout message to the other connection if the connection is disconnected. The code is as follows:

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 provides a Iappmodel custom feature that enables you to monitor user sessions, and the ability to process global messages through Appmodel, in a later article.

Client

EC also provides a convenient client function object, you just need to define simple code to send and receive the corresponding message to the corresponding server processing.

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 "Hello"});

With Xamarin we can also migrate functionality to different platforms, such as Android,ios, in the same way

private iservicechannel mclient = new Servicechannel ("10.0.2.2", 10034);p rotected Override void OnCreate (Bundle bundle) {base. OnCreate (bundle); Servicechannel.register (typeof (Mainactivity). Assembly);//Set our view from the "main" layout Resourcesetcontentview (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) and {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 a event to it} 

Such a multi-platform basic chat function is completed.

Sample code

Personal Open Source project Github.com/ikende

Elastic communication component for. Net

Easy to implement the chat server program based on Linux or win run

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.