Chat about online mobile games (2)

Source: Internet
Author: User

(2)
The Protocol for communication is actually a rule agreed between the client and the server for communication. Because both the client request and Server Response content are in the HTTP body, and the body is only a byte stream, the client and server must be consistent in understanding these byte streams.

In fruite-machine, Delimiter is used to represent a new line of character information. If the new character information needs to be isolated,/is used to isolate it.

Therefore, the entire sent message looks like this: Login logs drap logs secret

As an example, let's take a look at the interaction process between the player and the server after selecting a pet:
Midelt --------------------- Servlet
First, the MIDlet sends a rotation request to the servlet server. The request body contains the location of the selected PET and the flag (true or false) under the pet ).
After receiving the packet, the server processes the packet. And return the corresponding message according to the processing result. If the player wins, the server returns the player's winning position, the bonus points, and the position where the player stops after rotation. If it fails, the server returns a failed message to the player.

I will not talk about the client program. I will focus on the server program.
Next let's take a look at the overall structure:
When the fruitemachineservlet receives a request, it first analyzes the request from the mobile terminal or the web management page, and sends the request to the corresponding program for processing. Web page requests are mainly used to update databases. Mobile Terminal requests first analyze the request type: Login, game, or other ...... And give them to the corresponding program for processing. If it is a login, the game processor will retrieve the user's username and password from the database and verify the user. And generate a new httpsession session to manage the connection. If the user exits, the game logic will destroy httpsession.
First, let's look at the servlet program:
Import java. Io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;

Public class fruitmachineservelt extends httpservlet {
Private userdatabase;
Private adminprotocolhandler;
Private gameprotocolhandler;

Public void Init (servletconfig config) throws servletexception {
Super. INIT (config );
Userdatabase = new userdatabase ();

Userdatabase. createuser ("guest ","");

Adminprotocolhandler = new adminprotocolhandler (userdatabase );
Gameprotocolhandler = new gameprotocolhandler (userdatabase );
}

Public void doget (httpservletrequest request, httpservletresponse response)
Throws ioexception, servletexception {
Try {
String pathinfo = request. getpathinfo ();
If (pathinfo = NULL ){
Reponse. senderror (httpservletresponse. SC _bad_request, "missing Path Info ");
} Else if (pathinfo. startswith ("/admin ")){
Adminprotocolhandler. doget (request, response );
} Else {
Response. senderror (httpservletresponse. SC _bad_request, "unexpected Path Info ");
}
} Catch (ioexception e ){
E. printstacktrace ();
Throw E;
} Catch (exception e ){
E. printstacktrace ();
Throw new servletexception (E. getmessage ());
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.