Do the Project series (3): Mini QQ (1)-------enable clients to chat with each other

Source: Internet
Author: User

"Progressive Project series mini QQ article" will be introduced in the client chat, file transfer, plus friends, group chat, including voice chat, video chat, Remote Desktop and so on how to achieve the needs of interested friends can continue to pay attention to. Considering that some requirements are more complex, this series uses the mature communication framework esframework to do, rather than from the socket to start, of course, this and I caishuxueqian also have a great relationship, if we do not abandon the younger brother write too "low", also please hold personal field, by the way to give encouragement!

to get to the Chase , today is the simplest thing to do: clients chat with each other.

• Deployment of communication devicesSee Progressive Project series (1): The simplest C/s program--Let the server do the addition
        Static voidMain () {application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); irapidpassiveengine clientengine  = Rapidenginefactory.createpassiveengine (); // Create a client communication engine            ClientHandler ClientHandler = new clienthandler (); //creating a client Message Processor      Form_login LoginForm =NewForm_login (Clientengine, ClientHandler); Loginform.showdialog (); // Login window session complete to run main form Form_client Clientform =Newform_client (Clientengine, Loginform.selfuserid); Clienthandler.clientform=Clientform;        Application.Run (Clientform); }
Two • Landing window design
     Public Partial classForm_login:form {PrivateIrapidpassiveengine Clientengine; PrivateClientHandler ClientHandler;  Public string Selfuserid// I user ID, this property is primarily intended to expose the main form to identify users         {
Get{return This. Textbox_userid.text;} } PublicForm_login (irapidpassiveengine _clientengine, ClientHandler _clienthandler) {InitializeComponent () ; // incoming communication engine and message processor are primarily for communication engine initialization operations when logging in This. Clientengine =_clientengine; This. ClientHandler =_clienthandler; } Private voidButton_login_click (Objectsender, EventArgs e) { // Communication engine initialization, establishing a TCP connection to the target server Logonresponse Logonresponse = This.clientengine.initialize( This. Textbox_userid.text, This. Textbox_password.text,"127.0.0.1", -, ClientHandler); if(Logonresponse.logonresult = =logonresult.succeed) { This. DialogResult =DialogResult.OK; } Else{MessageBox.Show ("Login failed! "+ logonResponse.LogonResult.ToString () +logonresponse.failurecause); } } }
Three • Client main form Design
     Public Partial classForm_client:form {PrivateIrapidpassiveengine Clientengine; Private stringSelfuserid;  PublicForm_client (Irapidpassiveengine _clientengine,string_selfuserid) {             This. Clientengine =_clientengine;  This. Selfuserid =_selfuserid;  This. Text = _selfuserid; // Display this user ID on the form InitializeComponent (); }        Private voidButton_send_click (Objectsender, EventArgs e) {            stringmsg = This. Textbox_input.text; // chat messages byte[] Msgcode = Encoding.UTF8.GetBytes (msg); // transcoding for sending             stringTargetuserid = This. Textbox_targetuserid.text; // read outgoing user ID from input box  // send message with communication engine              This. ClientEngine.CustomizeOutter.Send            (Targetuserid, Informationtype.chat, Msgcode);  This. Showchatmsg ( This. Selfuserid, msg); }         // to display chat messages in a certain format on the interface          Public voidShowchatmsg (stringUseridstringmsg) {             This. Richtextbox_output.appendtext (UserID +"  "+ DateTime.Now.ToString () +"\ n");  This. Richtextbox_output.appendtext (msg +"\ n");  This. Richtextbox_output.scrolltocaret ();  This. Textbox_input.text =""; }    }
Four • Processing after the client receives the message
  Public classClienthandler:icustomizehandler {Privateform_client Clientform;  // This property is intended to inject clientform in the outer section and is not passed in the constructor because Clientform has not yet been created          Publicform_client Clientform {Get{returnClientform;} Set{Clientform =value;} }                Public void handleinformation(stringSourceuserid,intInformationtype,byte[] info) {            if(Informationtype = =informationtype.chat) {if( This. clientform.invokerequired) // is not the main  thread               {                    // convert to main  thread                    This. Clientform.invoke (Newesbasic.cbgeneric<string,int,byte[]> ( This.               handleinformation), Sourceuserid, Informationtype, info); }               Else               {                  stringmsg = Encoding.UTF8.GetString (info); // parsing messages                    This. clientform.showchatmsg (Sourceuserid, msg); // display message                }           }        }         Public byte[] Handlequery (stringSourceuserid,intInformationtype,byte[] info) {            Throw Newnotimplementedexception (); }
Five · Summary

Source Download :miniqq1.0.zip

as far as possible in the code to do the canonical name, and do a detailed comment, so no more to spend more text paragraphs to explain, I believe we can easily read. The whole logic is very simple, for beginners there are two points to note:

1. Each class is passed through the parameter to complete the cooperation, the method of transmission is through the constructor, some through the attribute injection, the novice friends can compare their own two ways to apply the occasion.

2. One of the refactoring is that the code is basically the same when chatting with both the message and the message received, so I refactor it. As for when to refactor, how to refactor, the novice friends can learn a lot of their own.

These two points are usually in the project often encountered problems, it is necessary to master.

Of course, the most important point is to do the project gradually ! Do more of these things will naturally be skilled, write the program can be write !

Do the Project series (3): Mini QQ (1)-------enable clients to chat with each other

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.