Document directory
- 1. Start the server engine in the WinForm Application
- 2. Start the server engine in the Console Program
The previous article has introduced all the key points of secondary development based on ESFramework/ESPlus. Now, we can start to try it out.
This article describes two Simplest Demos developed using the ESFramework Rapid engine, ESFramework. Demos. Simplest and ESFramework. Demos. Silverlight. The two demos are a simple chat system and demonstrate the following functions:
(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.
(6) Start the server engine in the console program.
1. Steps
Follow the secondary development steps mentioned in the article "ESFramework development manual (06) -- Rapid communication engine" to implement this demo step by step. (Please download the demo source code from the end of the article, and then compare it, it will be easier to understand)
1. Determine whether a friend relationship is required. In this demo, we assume that all online users are friends, so that we can directly use the DefaultFriendManager provided by ESPlus.
2. Determine whether group is required. In this demo, you do not need to broadcast messages or group messages. You can ignore the existence of the group mechanism.
3. Define information types and information protocols.
In this demo, we only need to define two types of information, one for chat messages and the other for clients to synchronously call the server. It is defined as follows:
Public static class InformationTypes
{
/// <Summary>
/// Chat Information
/// </Summary>
Public const int Chat = 0;
/// <Summary>
/// The client synchronously calls the server
/// </Summary>
Public const int ClientCallServer = 100;
}
Because the definitions of InformationTypes and the Information Protocol classes defined later are required on the client and server, we put them in a separate project ESFramework. demos. in Core, both the client and the server can reference it.
After the information type is defined, we will then define the information protocol.
For chat messages, as in addition to text, the message may also contain emoticon, we specifically define a Protocol Class: TextChatContract.
For synchronous calls, our demo assumes that the request information is a string, the reply information is also a string, directly use the UTF-8 encoding can be, do not need a special protocol class. Or, string is the protocol class we use.
4. Implement custom Information Processor
The MainForm of the client implements the ICustomizeHandler interface, which mainly implements the HandleInformation method to process received Chat information (the information type is InformationTypes. Chat ).
void HandleInformation(string sourceUserID, int informationType, byte[] info);
The CustomizeHandler of the server implements the ICustomizeHandler interface of the server. It mainly implements the HandleQuery method to process synchronous calls from the client (the information type is InformationTypes. ClientCallServer ).
byte[] HandleQuery(string sourceUserID, int informationType, byte[] info);
5. The server BasicHandler class implements the IBasicHandler interface to verify the account and password of the logged-on user. In this demo, if all the verifications are passed, the verification method returns true directly.
6. In the Initialize method of MainForm, the client schedules the connection status change events exposed by the Rapid Client Engine, such as disconnection, reconnection start, reconnection successful, and re-login. The status change events of IBasicOutter and IFriendsOutter are scheduled to handle online/offline notifications, dash notifications, and force disconnection notifications of other users. (For details, see basic functions and status notifications and friends and groups)
7. initialize the Rapid engine in the Main method of the Program class of the server and client.
The above describes the main components required for this demo. Let's take a look at the additional infrastructure required by the client and server.
2. Server description 1. Start the server engine in the WinForm Application
If you have obtained the formal authorization of ESFramework, you should set the correct authorization user account and password before initializing the server engine. The free account FreeUser is used in the demo ".
ESPlus.GlobalUtil.SetAuthorizedUser(AuthorizationVerifier.FreeUser, "");
The server directly uses the default main form MainServerForm provided by ESPlus to display online user status information.
// If you do not need the default UI display, replace the following sentence with your own Form.
ESPlus. Widgets. MainServerForm mainForm = new ESPlus. Widgets. MainServerForm (RapidServerEngine );
After the server is running, the interface is shown as follows:
(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 100. The "number of online users" on the rightmost shows the number of established connections-number of users.
(2) When a user logs in, the online user list displays the status of each user in real time.
At the end of this article, we will provide the MainServerForm source code. If you need it, you can modify and add functions to meet your application needs.
2. Start the server engine in the Console Program
In the solution, the ESFramework. Demos. ServerConsole Project is a console project used to demonstrate how to start the server engine in the console program.
For more systems, it may be required to Host the server engine in Windows Services. In fact, it is similar. I will not go into details here.
Iii. Client
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, directly exit the server. This will disconnect all clients, which is similar to the effect of disconnection. Restart the server and the client will be reconnected.
Finally, click the button next to "test request 123" to test synchronous message calling. Message synchronous calling is sent through the Query method of the ICustomizeOutter interface of the client, the synchronous call request will be processed by the HandleQuery method of the preceding server.
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.
4. Download source code
(1) ESFramework. Demos. Simplest source code
(3) MainServerForm source code
Read more ESFramework development manual articles.
Certificate -----------------------------------------------------------------------------------------------------------------------------------------------
For any questions about ESFramework, please contact us:
Tel: 027-87638960
Q: 372841921
Mail: esframework@oraycn.com