In the gaming industry, online games have a much larger market share than console games. And the user interaction of network game, user stickiness will be much better. As a result, most games require an Internet connection to store data, interact with players, and so on. In the project, two network operations were used, short connection HTTP and long connection Websocket, which are described in detail below.

Short connection HTTP

HTTP: Hypertext transfer protocol.

Here, clients are developed using objects, with the following common methods and properties:

1. Open () connects to the server and creates a new connection request.

2. Send () Sends a request to the server.

3, Abort () aborts the current request.

The readyState property provides the status of the current request, where 4 means ready.

5. The status property provides the current HTTP request status code, where 200 indicates that the request is successful.

ResponseText: the responseText returned by the server.

7, onReadyStatechange property, set callback function.

The open and Send functions, as well as the onReadyStatechange attribute, are key to HTTP requests.

The open function has five arguments to use. (method,url,async,user,password)

Method: indicates the request type, which can be GET or POST.

Url: The Url of the link to request.

Async: indicates whether an asynchronous request is made. This parameter is optional. The default value is true.

User: If the request requires authentication, specify the User name without default.

Password: Specifies the Password if the request requires authentication. No default value.

1. 5 ready states in XML HttpRequest

0: The request is not issued and is in this state until the open() function is called;

1: The request has been established but not sent, until send() is called.

2: The request is being processed.

3: The request has been processed, and some data is usually available in the response, but the server has not completed the response.

4: The response is complete and you can access the server response and use it.

2. Common HTTP status codes

401: indicates that the accessed data is not accessible.

403: Indicates that the accessed data is protected.

404: Indicates an incorrect URL request, indicating that the requested server resource does not exist.

200: everything is going well.

If the ready state is 4 and the status code is 200, the server’s data can be processed. I believe that those who have done Web development must be very familiar with this piece. If you have a question, you don’t have to worry about it. Then let’s look at the code.

3. GET request

4. A POST request

The editor

ReadyState =4 and status=200; readyState=4 and status=200; readyState=4 and status=200; Finally, don’t forget to send. Note: If the requested data is a string, it needs to be converted to JSON and can then be used as a table.

Of course, there are a few differences between A POST request and a GET request, such as parameters, Settings, etc.

In addition, to improve the user experience, we need to handle the request timeout accordingly, as shown in the following code.

5. The network request times out

Games with weak networking can use HTTP connection. If frequent interaction, real-time data acquisition and state synchronization are needed in the game, we generally use long-connection WebSocket.

Long connection to websocket

The specific use of WebSocket can refer to the following website, which will not be described here.

https://developer.mozilla.org/zh-CN/docs/Web/API/WebSocket.