ESFramework 4.0 Quick Start (08) -- getting started Demo, a simple IM system (with source code)

Source: Internet
Author: User

This article describes the simplest Demo developed using the ESFramework's Rapid engine. This Demo is a simple chat system and demonstrates the following features:

(1) When a client user is online or offline, other online users are notified.

(2) When the client and server are disconnected from the network, automatic reconnection is performed. After the network is restored, the reconnection is successful.

(3) All online users can chat in text.

(4) synchronous Message call.

(5) re-login mode. When users with the same name log on, the previous users will be squeezed out.

 

I. Server

The Server is very simple. You only need to implement the ESPlus. Application. CustomizeInfo. Server. ICustomizeInfoBusinessHandler interface to process synchronization requests from the client:

View Code

Public class CustomizeInfoBusinessHandler: ICustomizeInfoBusinessHandler
{
Public void HandleInformation (string sourceUserID, int informationType, byte [] info)
{
}

Public void OnTransmitFailed (string sourceUserID, string destUserID, int informationType, byte [] info)
{
}

/// <Summary>
/// Process the synchronous call request from the client.
/// </Summary>
Public byte [] HandleRequest (string sourceUserID, int requestInfoType, byte [] requestInfo)
{
If (requestInfoType = 100)
{
String requestMessage = System. Text. Encoding. UTF8.GetString (requestInfo );
String responseMessage = requestMessage + "already processed by the server! ";
Return System. Text. Encoding. UTF8.GetBytes (responseMessage );
}
Return null;
}
}

Then pass it into RapidServerEngine for initialization:

// For other types of authorized users, use the following statement to set the correct authorization user ID and password.
ESFramework. AuthorizationVerifier. SetAuthorizedUser (AuthorizationVerifier. FreeUser ,"");
Program. RapidServerEngine. Initialize (4530, new CustomizeInfoBusinessHandler ());

// If you do not need to display the UI, you do not need to call the following two statements:
ESPlus. Widgets. MainServerForm mainForm = new ESPlus. Widgets. MainServerForm ();
Program. RapidServerEngine. ConfigMainServerForm (mainForm );

After the program is started, the following interface is displayed:

(1) The bottom status bar shows the number of remaining threads in the thread pool. The initial number of backend thread pools and IOCP threads is 1000. The "number of online users" on the rightmost shows the number of established connections.

(2) When a user logs in, the online user list displays the status of each user in real time.

 

Ii. Client

1. The MainForm class of the client implements the ESPlus. Application. Basic. Passive. IBasicBusinessHandler interface to receive online/offline notifications, offline notifications, and heartbeat timeout notifications from other users.

2. The MainForm class of the client also implements the ESPlus. Application. CustomizeInfo. Passive. ICustomizeInfoBusinessHandler interface to process chat messages from other users.

3. The MainForm class of the client also schedules and processes events related to connection status changes of RapidPassiveEngine. TcpPassiveEngine, such as disconnection, reconnection start, and reconnection success/failure.

4. The ChatForm class is used to chat with other online users and Send messages to other users through the ESPlus. Application. CustomizeInfo. Passive. ICustomizeInfoOutter. Send method.

Void Send (string targetUserID, int informationType, byte [] info );

After the client starts to log on, the main interface MainForm is displayed as follows:

When another user logs in, the user list is displayed. Double-click the Avatar and a chat window is displayed.

When the window is unavailable, it indicates that the friends are offline.

If we log on to aa01 again (Demo re-login), the status bar of the original aa01 main interface will be displayed as follows:

If the Client and the server are not on the same machine, you can restore the network by disconnecting the network to observe the effect of automatic reconnection. the status bar of the main interface also has corresponding text prompts. Or simply click the "stop" button on the server interface to disconnect all clients, which is similar to the effect of disconnection. Then click Start to re-connect the client.

Finally, click the button next to "test request 123" to test synchronous message calling. Message synchronous calling is done through the CommitRequest method of ESPlus. Application. CustomizeInfo. Passive. ICustomizeInfoOutter interface:

Byte [] CommitRequest (int requestInfoType, byte [] requestInfo)

Like a method call, a request is sent to directly return a response. The synchronous call request is processed by the HandleRequest method of the server listed above.

 

It should be noted that this example is only a demo to demonstrate how to use the functions provided by ESFramework. For simplicity, a lot of work is omitted, for example, the Rapid engine instance is used as a static member of the Program, and the MainForm class is relatively large by implementing multiple interfaces. In a formal project, you should make a proper structure design based on the actual situation of the project. Do not follow this example.

 

Iii. Download Demo source code

As many of my friends are using the VS2005 development environment, this Demo is specially developed using VS2005 and can be directly upgraded using VS2008 or VS2010.

In addition, this Demo can be directly deployed and tested on the public network.

Download the Demo source code.

With ESFramework, you will find it so easy to develop an IM system.

This article demonstrates the first and simplest demo, and the subsequent advanced demo. We will demonstrate how the Silverlight client performs IM chat with the client in this example, the subsequent Demo will demonstrate functions such as file transfer. Thank you.

 

 

ESFramework 4.0 Overview

All articles in ESFramework 4.0 Quick Start

All articles in ESFramework 4.0 advanced edition Series

 

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.