Development Program: Crazy Bombs
Development progress: Phase II
The main point of this issue: Design Client Network features
Development platform: Java Platform
Last period we talked about the client game function, the design of the program is only a single game, why is this? Unable to communicate because there is no network capability. This period, we will introduce the design of the client network function.
Design idea of client network function
Client network function is to convert the operation of the client to the server side, and the data of server-side feedback is presented to the user in the form of interface. If you put all the logical processing on the server side, of course, the most secure form, but so that the server side of the pressure will be relatively large, so the general will be some simple program logic, such as this example when moving the boundary detection, placed on the client to judge.
To design the client network function, the instructions to be used are: Prepare (READY), start (start), move, get game data (GETDATA) and Release Bomb (Laybomb). Send a prepare instruction on the first connection, obtain a unique user number (UserID) from the server side, and then send the start command to the server to see if any other users are ready to play.
If not, the start instruction is circulated, if there is a user, the room number is assigned on the server side, then a new room is opened on the server side, and the game initialization data is fed back to the client, and the client enters the game interface. In the game interface, the game data is requested once every 1 seconds from the server side, which contains the room number and the user number, and when the user moves, sends a move command to the server, sending a release bomb command to the server when the bomb is released (Figure 1).
Network protocol Design
The network protocol (PROTOCOL) is the core of the network program, and it is the secret of the network program. Network protocol refers to the network transmission data format, composed of two sets of formats: the client sent data format and server-side data format sent. The data format sent by the client is the same as the data format received by the server side, and the data format received by the client is the same as the data format sent by the server.
In this program, the client needs to send a smaller number of instructions, format is also not complex, so the common "instruction name + parameter" format design, that is, the transmission of data in the beginning of a data to represent the type of instruction, and the required parameters of the instruction in a certain format followed by the instruction, To form a complete protocol data. With detailed introduction, please go to Http://www.shudoo.com/bzsoft to download.
Listener Mode Analysis
In order to combine the client interface and client network programming effectively, and reuse the network programming part of the code, a better way is to use the "listener mode." Listener mode is when a class execution function reaches a step, and the functional code that handles the step is delegated to another object for processing.
For example, an alarm on a car uses a listener mode, and when the car is moved, the alarm object is processed by making a sound. In the network function, also uses the listener mode, the network communication thread entrusts the server-side data feedback to the interface class method to handle, thus facilitates the interface control, avoids the massive data transmission.
In this example, a HttpListener interface is declared, the class that implements the interface has the ability to handle the feedback data of the network communication, and the Doresponse method in the interface is invoked when the data feedback in the network communication is processed. And each class that needs network communication implements the HttpListener interface, then each interface class has the ability to handle the server-side feedback data, then carries on the processing of the feedback data inside the Doresponse method that realizes.
Client Network function Design steps
First step: Create a project
Start the Eclipse environment, select "File→new→j2me MIDlet Suit", set the project name "Bomber", and select "Next" to complete the project setup. Then create a new J2ME entry class, select "File→new→j2me MIDlet", set the name "Bombermidlet", and click "Finish". Finally, follow the steps described in the previous period to write the interface code and key processing code (code download address: Http://www.shudoo.com/bzsoft)