Breeze IM 3.3 series one to achieve new user registration, im New User Registration
The logon window is as follows:
This article first implements the "register a new account" function"
Step 1:
Open VS2010 and find the BasicForm form class. This class is a base class for beautifying the form. We can inherit from this form.
Step 2: create a new form
RegisterUser. cs
Change the Form class inherited by the new Form RegisterUser to the BasicForm class.
For example:
Now you can use the BasicForm style for the new form. The effect is as follows:
Drag the control. The interface is as follows:
Step 3:
Let's review the corresponding tables in the database:
We know that when TCP communication is used, the client interacts with the server, instead of directly interacting with the database. Instead, the client communicates with the server, and the server then communicates with the database.
Therefore, the procedure for registering a user is
(1): On the client registration page, send the user's desired "User ID", "nickname", and "password" to the server through TCP communication.
(2): The server verifies and interacts with the database to determine whether the user ID already exists. If the user ID already exists, the server sends a message to the client. "The user already exists.
If the "user ID" meets the requirements, insert it into the database table and return a message to the "client" to inform it that the registration is successful.
(3) "User ID", "nickname", and "password" must be stored in a class. Here we use the RcUsers data table's corresponding entity class: RcUsers, which supports Protobuf.net serialization.
(Through TCP communication, data classes are transmitted between the client and the server. classes must be serialized into binary data. After receiving the data, the other party deserializes the data. We use the open-source protobuf.net serializer on the Internet.
Of course, the deserialization work is completed by the open-source networkcomms2.3.1 framework)
Using System; using System. collections; using System. collections. generic; using System. data; using ProtoBuf; using SimpleIM. data; namespace SimpleIM. business {[ProtoContract] public class RcUsers {# region Constructors public RcUsers () {}# endregion # region Private Properties private int id =-1; private string userID = string. empty; private string name = string. empty; private string password = string. empty; private string declaring = string. empty; private int status =-1; private int groupID =-1; private bool isMale = false; private int userLevel =-1; private bool enabled = false; private DateTime registerTime = DateTime. utcNow; private DateTime lastLoginTime = DateTime. utcNow; # endregion # region Public Properties [ProtoMember (1)] public int Id {get {return id;} set {id = value ;}} [ProtoMember (2)] public string UserID {get {return userID;} set {userID = value ;}} [ProtoMember (3)] public string Name {get {return name ;} set {name = value ;}[ ProtoMember (4)] public string Password {get {return password;} set {password = value ;}} [ProtoMember (5)] public string Declaring {get {return declaring;} set {declaring = value;} [ProtoMember (6)] public int Status {get {return status ;} set {status = value ;}[ ProtoMember (7)] public int GroupID {get {return groupID;} set {groupID = value ;}} [ProtoMember (8)] public bool IsMale {get {return isMale;} set {isMale = value;} [ProtoMember (9)] public int UserLevel {get {return userLevel ;} set {userLevel = value ;}[ ProtoMember (10)] public bool Enabled {get {return enabled;} set {enabled = value ;}} [ProtoMember (11)] public DateTime RegisterTime {get {return registerTime;} set {registerTime = value;} [ProtoMember (12)] public DateTime LastLoginTime {get {return lastLoginTime ;} set {lastLoginTime = value ;}# endregion }} RcUsers classView Code
The RCUsers class is generated by the CodeSmith template. For details, refer to other related articles about CodeSmith in this blog.
NetworkComms network communication framework supporting CodeSmith template introduction template: [Download TEMPLATE] share my database framework
In breeze IM, the stored procedures, data-Layer Code, Entity classes, and logic-Layer Code are all generated by using the CodeSmith template (corresponding modifications are required to add features ), this greatly improves work efficiency.
Let's get down to the point, and we start registration-related development:
Client code:
// Define a contract class to pass relevant information to the server RcUsers currentUser = new RcUsers (); // assign currentUser to the attribute. userID = txtUserID. text. trim (); currentUser. password = txtPsw. text. trim (); currentUser. name = txtNickName. text. trim (); // synchronous method transfer and serialize the contract class to the server, and obtain the returned information from the get (the returned information type is ResMessage) // type of message sent by RegUser // ResMessage regContract = Common. tcpConn. sendReceiveObject <ResMessage> ("RegUser", "ResRegUser", 8000, currentUser); if (regContract. message = "registered") {BasicMsgBoxForm = new BasicMsgBoxForm ("registered successfully", "Information Window", MessageBoxButtons. OK, MessageBoxIcon. none); form. startPosition = FormStartPosition. centerScreen; form. showDialog (); txtNickName. text = ""; txtUserID. text = ""; txtPsw. text = ""; txtRePsw. text = "";} else {BasicMsgBoxForm form = new BasicMsgBoxForm (regContract. message, "Information Window", MessageBoxButtons. OK, MessageBoxIcon. error); form. startPosition = FormStartPosition. centerScreen; form. showDialog ();}
Using System; using System. collections. generic; using System. text; using ProtoBuf; namespace SimpleIM. business {// return information of the operation result, which can be [ProtoContract] public class ResMessage {[ProtoMember (1)] public string Message; public ResMessage () {} public ResMessage (string message) {this. message = message ;}}}
Server code:
// Register the new user NetworkComms. AppendGlobalIncomingPacketHandler <RcUsers> ("RegUser", IncomingRegisterUser );
// Register a new user private void IncomingRegisterUser (PacketHeader header, Connection connection, RcUsers contract) {try {// declare a contract class for returning data to the client ResMessage resMessage = new ResMessage (); // determine whether the user has RcUsers user = DoRcUsers. getByUserID (contract. userID); if (user. id>-1) {// If yes, the client resMessage is directly returned without database operations. message = "User ID already exists";} else {// if not, add the user to the database
// Modify the user information. You can modify the information as needed.
Contract. declaring = "Spring blossom"; contract. status =-1; contract. isMale = true; contract. userLevel = 1; contract. enabled = false; contract. registerTime = DateTime. now; contract. lastLoginTime = DateTime. now;
// A contract group is specified here for convenience. groupID = 115; // Add the user to the database. save (contract); resMessage. message = "successfully registered";} // return information to the client for connection. sendObject ("ResRegUser", resMessage);} catch (Exception ex) {LogTools. logException (ex, "IncomingChangePsw ");}}
As follows:
Now you can see the newly registered user on the home page.