This article will surround the three topics to tell you about the network interconnection implementation method of online games.
There is a significant difference between online games and stand-alone games, which is a server for connecting all users in addition to a user interface platform (such as single game) for operation games, and provides data services for all users, From some point of view, the game server is like a large database, providing data and data logic interaction. Let's take a look at a simple online game model execution process:
Client computer:
Login () // login module {initialization game data; get the user and password entered by the user; create a network connection with the server; send it to the server for user authentication; ... Waiting for the server to confirm the message; ... get the server feedback login message IF (established) enters the game; ELSE prompts the user to log in error and re-accept the user login;} Game () // Game cycle part {Draw game scene, characters, and other elements; get user operation input; send users to the server to the server ; ... Waiting the server's message; ... receive feedback information of the server; Switch (Message Data of Server Feedback) {CASE Local Player Mobile message: {IF (allow local player mobile) Client to process people's movement; Else Customer The machine maintains the original state;} Break; Case other player / NPC mobile message: {Other players or NPC mobile processing according to the feedback information of the server;} Break; Case new player joins the game: {Add this in the client Players;} Break; Case players leaving the game: {Destroy this player data in the client;} Break; ... other message type processing; ... default: Break;}} EXIT () // Game end {send leave Message to the server; ... Waiting for the server to confirm; ... get the server to confirm the message; disconnect with the server; release the game data; leave the game;
server:
Listen () // Game Server Waiting for the player connection module {... Waiting for the user's login information; ... receive user login information; analyze if the username and password are compliant; if (conformity) {Send a confirmation allowed to enter the game message to customers Machine; put this player into the game message to all players in the scene; add this player to the server scene;} else {Disconnect the connection with the client;}} game () // Game server loop section {.. . Waiting for the player's operation input; ... receives a player's mobile input or NPC mobile logic input; // This player / NPC is used as an example to perform logic judgment that the player / NPC is movable; IF (movable) {Move the player / NPC to the server; send the mobile message to the client; send this player's mobile message to all players on the scene;} Else Send an unmovable message to the client;} exit () / / Game Service = Merger End {Received Players Leave Messages; Send this message to all players in the scene; send the information allowed to leave; deposit the player data into the database; log out of the data in the server;}}
Let us explain the operation mechanism of the simple online game model. Let's talk about the server side, here the server is divided into three parts (actually a complete online game far more than this): login module, game module, and logout module. The login module is used to listen to the network connection message sent by the online game client and verify its legitimacy, then create this player in the server and lead the player to the game module; the game module provides the player's actual application service, We will introduce this section in detail; after getting the player wants to leave the game, the logout module will remove the player from the server and save the player's attribute data into the server database, such as: Experience, Level, Life value, etc. Let's take a look at the client of online games. At this time, the client is no longer like a stand-alone game. After initializing the data, you can enter the game directly, but to create a connection with the server, and enter the game before the license is obtained. In addition, the client game process of online games needs to communicate with the server, and the status of the current game is determined by switching data with the server, such as the position of other players, the item drop. Similarly, when leaving the game, the client will inform the server to leave the player user to make the server proceed accordingly. The above use simple pseudo code to elaborate the execution process of stand-alone games and online games, and everyone should clearly see the differences between the two, and between the two mutual relationships. We can consider it, the online game is to move the logical arithmetic part of the stand-alone game to the game server and then return the processing result (including other player data) to the player to the connected player through the game server.