[Unity] Using Socket/TCP/IP to develop network game framework

Service-Terminal

1. First, create a .NET FrameWork console application in VS2017, and use this as the basis for the server.

2. Configure the solution, the default class name should not be Chinese

3. Create 5 folders, namely Server, Tool, Model, DAO and Controller.

  Server handles connection and communication functions.

  Model stores data tables.

  DAO handles operations on the database.

  The Controller handles the response logic for different client requests.

  Create a Program class with a Main method.

4. Create the following classes in the Server folder:

 [Discovery] Namespace default: the same as the project name. Subnamespace default: projectname.foldername. (don't repeat the class name)

    Server: Handles the monitoring of the server and manages the list of connected clients. Receive messages from clients, and send messages to clients.

 (Generate an object of the Server class in the Main method)

    Client: Handles client monitoring and receives messages from the server.

    Message: Responsible for message transmission between the server and the client, providing an interface for data arrays.

5. In the Controller folder:

   (1) Abstract class: BaseController

   (2) ControllerManager (held by Server, accessed by client through Server)

            Responsible for saving all Controllers and calling corresponding processing methods according to ControllerCode and ActionCode.

   (3)DefaultController、

6. Create a public project Common, and other projects reference this project.

   Create an enumeration of RequestCode, ActionCode in it

7. In the Tool folder:

   (1) ConnectHelper class: responsible for connecting to the database and calling related methods in the Client class.

            Note: To import the dll first: C:\Program Files (x86)\MySQL\Connector NET 8.0\Assemblies\v4.5.2\MySql.Data.dll

8. In the Model folder:

  (1) Design the User table and Result table that the game needs to use in the MySQL database.

  (2) Create a corresponding class for each table

client

1. Manage all Managers (inherited from BaseManager) through GameFacade (singleton mode), and are responsible for their mutual access.

   In the Start method, the various objects are generated and their initialization methods are executed.

   In the OnDestroy method, execute the destruction method of each object.

2. Managers

 (1) BaseManager: OnInit, OnDestroy methods, which are convenient for subclasses to rewrite.

 (2)UIManager:

 (3)ClientManager:

    A. Initialization: Generate a socket object and connect to the server. Receive messages from the server.

    B. Destroy: close the connection

   Create the Message class: The format of the client sending and receiving messages to the server is reversed.

    Introduce Common dll. Generated in VS, it should be noted that the .Net Framework target should be in version 3.5 and below. After generation, find the dll in the bin/Debug folder and drag it to the Plugins folder of Unity.

    C. Send request: packData, SendMessage

    D. Receive responses: ReadMessage, HoldResponse (distributed to different Requests) 

3.Request:

(1) Use RequestManager to manage (dictionary) all Requests (all inherited from BaseRequest)

         HoldResponse method: According to the RequestCode passed by the Client, call the OnResponse function corresponding to the Request

(2) LogRequest: Login: After entering the user name and password, connect to the database for detection

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325563248&siteId=291194637