This article describes how to use the Rapid engine to quickly develop ESFramework. This article describes how to use ESPlus. application. the classes in the mizeinfo namespace can send and process custom messages. This article uses a simple example to explain how to use custom messages.
The example scenario is very simple: assume that the client requires a request to join a group after logging on to the server, the server receives the request, processes the request, and replies to the client-whether the request is added successfully, after receiving the reply, the client can handle it accordingly.
1. Define the Message Type and Message Protocol Contract
This scenario involves two types of messages: request to join a group (send the client to the server) and result of adding the group (the server replies to the client ). Therefore, we can define these two message types:
Public static class InformationTypes
{
/// <Summary>
/// Request to join the group Client = Server. The corresponding message body is JoinGroupContract /// </summary>
Public const int JoinGroup = 1;
/// <Summary>
/// Result Server = "Client added to the group. The corresponding message body is JoinGroupResultContract.
/// </Summary>
Public const int JoinGroupResult = 2;
}
In addition, we can encapsulate the requests sent from the client to the server and the responses sent from the server to the client in the Protocol Class JoinGroupContract and JoinGroupResultContract respectively:
[Serializable]
Public class JoinGroupContract
{
Public JoinGroupContract (){}
Public JoinGroupContract (string _ groupID, string _ groupPwd)
{
This. groupID = _ groupID;
This. groupPwd = _ groupPwd;
}
# Region GroupID
Private string groupID = "";
/// <Summary>
/// ID of the group to which the request is sent
/// </Summary>
Public string GroupID
{
Get {return groupID ;}
Set {groupID = value ;}
}
# Endregion
# Region GroupPwd
Private string groupPwd = "";
/// <Summary>
/// Request the password for joining the group
/// </Summary>
Public string GroupPwd
{
Get {return groupPwd ;}
Set {groupPwd = value ;}
}
# Endregion
}
[Serializable]
Public class JoinGroupResultContract
{
Public JoinGroupResultContract (){}
Public JoinGroupResultContract (bool _ succeed)
{
This. succeed = _ succeed;
}
# Region Succeed
Private bool succeed = true;
/// <Summary>
/// Whether the group is successfully added
/// </Summary>
Public bool Succeed
{
Get {return succeed ;}
Set {succeed = value ;}
}
# Endregion
}
Of course, the Protocol class here is very simple, and for our actual application, it is okay to encapsulate a lot of information in the Protocol class.
2. The client sends a request to the server
After the engine Initialization is successful, the client can send a request to the server to join the G001 group:
// Request to the server to join the G100 Group
JoinGroupContract = new JoinGroupContract ("G100", "123456 ");
RapidPassiveEngine. CustomizeInfoOutter. Send (InformationTypes. JoinGroup, ESBasic. Helpers. SerializeHelper. SerializeObject (JoinGroupContract ));
3. The server processes the request and sends a reply to the client
The Server processes the request by implementing the HandleInformation method of the ESPlus. Application. CustomizeInfo. Server. ICustomizeInfoBusinessHandler interface:
Private ICustomizeInfoController customizeInfoController =...; // The server processes messages.
Public void HandleInformation (string sourceUserID, int informationType, byte [] info)
{
If (informationType = InformationTypes. JoinGroup)
{
JoinGroupContract joinGroupContract = (JoinGroupContract) ESBasic. Helpers. SerializeHelper. DeserializeBytes (info, 0, info. Length );
String groupID = joinGroupContract. GroupID;
// ...... (Logical processing)
JoinGroupResultContract resContract = new JoinGroupResultContract (true); // agree to join
// Send a reply message to the user
This. customizeInfoController. Send (sourceUserID, InformationTypes. JoinGroupResult, SerializeHelper. SerializeObject (resContract ));
Return;
}
}
4. The client processes server replies
Next, the client can also process the server reply by implementing the HandleInformationFromServer method of the ESPlus. Application. CustomizeInfo. Passive. ICustomizeInfoBusinessHandler interface:
// The client processes the message
Public void HandleInformationFromServer (int informationType, byte [] info)
{
If (informationType = InformationTypes. JoinGroupResult)
{
JoinGroupResultContract resContract = (JoinGroupResultContract) SerializeHelper. DeserializeBytes (info, 0, info. Length );
If (resContract. Succeed)
{
//... Joined successfully.
}
Return;
}
}
This demo briefly demonstrates the definition of the message type, the definition of the Protocol class, and a complete process in which the client sends a request to the server, the server processes the request, and replies to the client. Through custom messages, we can implement very complex collaboration between the server and the client. Therefore, even if we use the ESFramework's quick engine, we can do a lot of complicated things.
Note that the smaller the size of the message, the smaller the message at the same frequency, and the higher the communication efficiency. In this demo, we use. NET binary serializer converts the Protocol object to byte []. Using this serializer, the byte [] is usually very large, and the larger the message, this will increase the burden of communication between the server and the client. Therefore, if you have a better and more concise serialization method, we recommend that you do not use the binary serializer that comes with. NET.
ESFramework 4.0 Overview (the latest version of ESFramework4.0 is included at the end of this article, and related demos and help documentation download)
All articles in ESFramework 4.0 Quick Start
All articles in ESFramework 4.0 advanced edition Series