Communication Framework for Networkcomms 2.3.1
When using TCP communication, the client sends a message to the server, such as a message is the login message B message is the update password message server side how to distinguish A and B, and call the appropriate processor?
The NETWORKCOMMS communication framework uses this mechanism: the client assigns a "message type" to each message, and the server side finds the appropriate processor based on the "message type".
Take "login Message" as an example
Client code:
//Save the user name and password in the contract classUserInfo UserInfo =NewUserInfo (); Userinfo.userid=TxtUserID.Texts.Trim (); Userinfo.password=TxtPassword.Texts.Trim (); //send the contract class to the server side and get the returned resultsUserlogincontract logincontract = newtcpconnection.sendreceiveobject<userlogincontract> ("Userlogin","Resuserlogin",8000, UserInfo); //If the login is successful if(Logincontract.message = ="Success") {
Do the next steps
}
UserInfo is a serializable class of contracts that can be used to pass information
"Userlogin" is the message type that we specify, the server side receives this type of message, calls the appropriate processor to process it, and, by convention, returns a message of the "Resuserlogin" message type.
The actual type of message sent is UserInfo
The actual type of message returned is Userlogincontract
Server-side code:
(1) The processor that declares the corresponding message type
Networkcomms.appendglobalincomingpackethandler<userinfo> ("userlogin", Incomingloginhandler);
(2) The specific processor
//Note The parameter type of the third parameter of the processor is consistent with the actual type of message sent by the client
Private voidIncomingloginhandler (packetheader header, Connection Connection, UserInfo UserInfo) {Try{userlogincontract rescontract;//Verifying logon information from the databaseRescontract =Dorcusers.login (Userinfo.userid, Userinfo.password);
The //Return message gives the client the actual data type of the message returned by the same connection as the data type that the client waits to get when it sends the message . SendObject ("Resuserlogin", rescontract);
} Catch(Exception ex) {logtools.logexception (ex,"Incomingloginhandler"); } }
www.networkcomms.cn Edit
Breeze IM 3.3 Series three client-to-server communication modes