COCOS-JS HTTP mode network request

Network structure

Network structure is the way of network construction. Currently there are client-server structure (C/S structure) and peer-to-peer (P2P) structure network. .

Client server structure (C/S structure)

This structure is also called the Clicent/Server structure, which is a Master-slave structure. The server is always in a waiting state. If the client requests, the server responds to the request, establishes a connection, and provides services. The server is passive and the client is active. Similar to ordering in a luxury restaurant, the customer is active and the waiter is passive.

point to point Structure (P2P structure)

This structure is also called peer-to-peer structure network , each node is equal. The network distribution range is relatively small, usually in an office or a home, suitable for communication between mobile devices, and the network link layer is implemented by Bluetooth and Wifi. Similar to eating a luxurious buffet, all guests are equal.

Considering the needs of cross-platform, the Cocos-JS engine mainly uses the C/S network structure. This structure mainly uses transmission protocols such as HTTP and HTTPS.

HTTP

HTTP is a hypertext transfer protocol, and the basic protocol of Interent is TCP/IP. Currently Widely used are HTTP, HTTPS, FTP, Archie Gopher, etc. which are application layer protocols on top of TCP/IP. Different protocols correspond to different applications.

HTTP is an object-oriented protocol belonging to the application layer. Because of its concise and fast method, it is suitable for distributed hypertext information transmission.

HTTP defines 8 request methods, OPTIONS, HEAD, GET, POST, PUT, DELETE, TRACE and CONNECT as a web server. At least GET and HEAD methods must be implemented. Other methods are optional.

The GET method is to send a request to the specified resource. The information sent is behind the URL. It is like using a postcard to write a letter to others, and the “letter content” is written on the outside so that everyone in contact can see it, which is not safe.

POST method is to submit data to the specified resource, request The server performs processing. It’s like putting the “letter content” in an envelope and sending it out, so it’s safe for anyone in contact with it to see it.

HTTPS

HTTPS is a secure hypertext transfer protocol, a combination of hypertext transfer protocol and SSL encryption. Provide encrypted communication to authenticate the identity of the network server.

HTTPS is an upgraded version of HTTP. The difference with HTTP is that HTTPS uses https:// instead of http://, HTTPS uses port 443, and HTTP uses port 80 to communicate with TCP/IP Venus. SSL uses 40-bit keywords as the RC4 stream encryption algorithm. This is suitable for the encryption of commercial information.

Use XMLHttpResquest object

In the web front-end development, there is an asynchronous refresh technology AJAX. Its core technology is the XMLHttpResquest of the JavaScript object, which is a asynchronous request technology.

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

2. send () to send a request to the server.

3. abort() Exit the current request.

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

5. The status attribute provides the current HTTP request status, where 200 means the request was successful.

6. responseText property, the response text returned by the server.

7. Onreadystatechange property, set the callback function.

Among them, the open and send functions, and the onreadystatechange attribute are the key to the http request.

The open function has 5 parameters that can be used. (Method, url, async, user, password)

method refers to the type of request sent, generally GET or POSt

url refers to the requested link

p>

Whether async is an asynchronous request, this parameter is optional, the default is true

user If the request requires identity authentication, specify the user name here, no default value

password if The request requires authentication. The password is specified here, and there is no default value.

XMLHttpResquest has 5 ready states

0: The request was not issued, and the open( ) Before the function, the status is changed.

1: The request has been established but has not been sent, and the status is changed before calling the send() function.

2: The request has been sent and is being processed

p>

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

4: The response has been completed, you can access the server response and use it

Common HTTP status codes

401: indicates that the accessed data is forbidden to access

403: indicates that the accessed data is protected

404: indicates the wrong URL request, the server resource does not exist

200: indicates the request is successful

If yes The status is 4, and the status code is 200, indicating that the request is complete and the server data can be processed

Get request method

 1 var XMLHttpResquestGet = function (url, callback) {

2 var request = cc.loader.getXMLHttpRequest();
3 request.open("GET", url, true span>);
4
5 request.onreadystatechange = function () {
6 //If the ready status is 4 and the status code is 200, the request is successful
7 if(request.readyState === 4 && request.status === 200) {
8 try{
9 console.log("XMLHttpResquestGet Status:" + request.statusText );
10 if(callback) callback(true, request.responseText);
11 }
12 catch (e) {
13 if(callback) callback(false, e);
14 }
15 }
16 else{
17 if(callback) callback(false, );
18 }
19 };
20 request.send();
21 };

POST request Method

 1 var  XMLHttpResquestPost = function (url, params, callback) {

2 var xhr = cc.loader.getXMLHttpRequest();
3 xhr.open("POST", url);
4
5 xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
6 xhr.onreadystatechange = function () {
7 //If the ready status is 4 and the status code is 200, the request is successful
8 if(xhr.readyState === 4 && xhr.status === 200) {
9 try{
10 console.log("XMLHttpResquestPost Status:" + xhr.statusText );
11 if(callback) callback(true, xhr.responseText);
12 }
13 catch (e) {
14 if(callback) callback(false, e);
15}
16 }
17 else{
18 if(callback) callback(false);
19 }
20 };
21 xhr.send(params);
22 };

 1 var XMLHttpResquestGet = function (url, callback) {

2 var request = cc.loader.getXMLHttpRequest();
3 request.open("GET", url, true span>);
4
5 request.onreadystatechange = function () {
6 //If the ready status is 4 and the status code is 200, the request is successful
7 if(request.readyState === 4 && request.status === 200) {
8 try{
9 console.log("XMLHttpResquestGet Status:" + request.statusText );
10 if(callback) callback(true, request.responseText);
11 }
12 catch (e) {
13 if(callback) callback(false, e);
14 }
15 }
16 else{
17 if(callback) callback(false, );
18 }
19 };
20 request.send();
21 };

 1 var XMLHttpResquestPost = function (url, params, callback) {

2 var xhr = cc.loader.getXMLHttpRequest();
3 xhr.open("POST", url);
4
5 xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
6 xhr.onreadystatechange = function () {
7 //If the ready status is 4 and the status code is 200, the request is successful
8 if(xhr.readyState === 4 && xhr.status === 200) {
9 try{
10 console.log("XMLHttpResquestPost Status:" + xhr.statusText );
11 if(callback) callback(true, xhr.responseText);
12 }
13 catch (e) {
14 if(callback) callback(false, e);
15}
16 }
17 else{
18 if(callback) callback(false);
19 }
20 };
21 xhr.send(params);
22 };

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3643 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.