There are many games that require network communication, a client-server fabric network, and a point-to-point fabric network. Considering the need of cross-platform, COCOS2D-JS engine mainly uses C/s structure network. In general, the peer structure network uses Bluetooth communication, and the specific platform generally provides the local API for accessing peer. such as the game Kit for iOS, but these APIs cannot be used on COCOS2D-JS engines with cross-platform features.Network StructureNetwork structure is the way of network construction, there is a popular client server structure network and point-to-point structure network.Client Server Fabric NetworkThe client server structure network is a master-slave structure network. The server is generally in a wait state, and if there is a client request, the server responds to the request, establishes the connection, and provides the service. The server is passive and the client is active.Point-to-point structured networksA point-to-point fabric network is also called a peer-structured network, and each node is peer-aligned.http and HTTPSThe most important application layer of client server is the transport protocol such as HTTP and HTTPS. It is therefore necessary to introduce HTTP and HTTPS. 1.HTTPHTTP is a Hypertext Transfer Protocol, the basic protocol of the Internet is TCP/IP, the current widely used is HTTP, HTTPS, FTP, Archie Gopher, etc. is based on TCP/IP Application layer protocol, different protocols corresponding to different applications. HTTP is an object-oriented protocol that belongs to the application layer. Because of its concise, fast way. Suitable for distributed hypertext information transmission. The HTTP protocol defines a total of 8 request methods, Options,head,get,post,put,delete,trace and connect. As a Web server, at least the GET and HEAD methods need to be implemented, and other methods are optional. The Get method sends a request to the specified resource, and the information sent is displayed behind the URL. The Get method should only be used to read data, such as static images. The Post method submits data to the specified resource and requests the server to process it. The Get method is like using a postcard to write to someone, "the content of the letter" is written outside, accessible to anyone can see, unsafe. The Post method is like putting the "letter content" into an envelope and writing it to someone else, and it is safe to see the person they are exposed to.HTTPSHTTPS is a secure text transfer protocol that is a combination of Hypertext Transfer Protocol and SSL. Provides encrypted communication and authentication of the identity of the network server. To put it simply. HTTPS is an upgraded version of HTTP. The difference from HTTPS is that HTTPS uses https://instead of Http://,HTTPS to use port 443, while HTTP uses port 80 and TCP/IP for communication. SSL uses 40-bit keywords as the RC4 stream encryption algorithm. This is appropriate for the encryption of business information. HTTPS and SSL support use of the digital authentication of the. If necessary, the user can confirm who the sender is.developing clients using the XMLHttpRequest objectThere is an asynchronous refresh technique-ajax in Web front-end development. At the heart of Ajax is the JavaScript object XMLHttpRequest, which was first introduced in Internet Explore5, a technique that supports asynchronous requests. With the help of the XMLHttpRequest object, the JavaScript language can be used to make requests to the server and handle responses.using the XMLHttpRequest objectThe COCOS2D-JS engine ported its platform because HTTP requests were made using the XMLHttpRequest object in the Web and in general. XMLHttpRequest objects can be used in Cocos2d-js JSB on-premises platforms and cocos2d-html web platforms. The functions and properties commonly used in XMLHttpRequest objects are as follows
Where the open and send functions, as well as the onReadyStateChange property, are key to the HTTP request. The Open function has 5 parameters that can be used. (1) The type of request-type send request. The typical value is get or post. You can also send a head request. (2) URL URL to request a connection (3) Asynch True if you want to use an asynchronous connection, otherwise false. This parameter is optional and defaults to True (4) Username: If authentication is required, you can specify the user name here, and the optional parameter has no default value. (5) Password: If authentication is required, you can specify the password here, and the optional parameter has no default value. There are 5 readiness states in the XML HttpRequest: (1) 0: The request is not issued before the open () function is called (2) 1: The request has been established but has not yet been issued, in the call to send () function before the state (3) 2: The request has been issued in Process (4) 3: The request has been processed, the response is usually partially available, but the server has not completed the response (5) 4: The response is complete, you can access the server response and use it in line 8th of the Code xhr.status== 200 determine the HTTP status code. The common HTTP status code is as follows: (1) 401: Access Data Forbidden (2) 403: Indicates that the data accessed is protected (3) 404: A URL request that represents an error, The server resource that represents the request does not exist (4) 200: means everything goes well if the ready state is 4 and the status code is 200 you can process the server's data. The 9th line of code obtains the data returned from the server through the ResponseText property of the XMLHttpRequest object. The 10th line of code uses the Send () function to send dataData Interchange FormatThe Data Interchange format is the "voice" of a conversation between two programs. The data Interchange format is like two people chatting, using a language that can be understood by each other. The data Interchange format is mainly divided into CSV format, XML format and JSON format. The CSV format is a simple comma-separated interchange method. Raw data:
XML data format:
JSON data format:
Document Structureform the two structures of a JSON document: objects and arrays. The object is a collection of name/value pairs, similar to the dictionary type in obejective-c. An array is a collection of a series of elements. An array is an ordered collection of values, and an array begins with "[" (the left square bracket), "]" (the right square bracket), and the values are separated by "," (comma). Values in JSON objects and arrays can be strings, numeric values, true, FALSE, null, objects, or arrays in double quotation marks, and these structures can be nested.JSON decoding and encodingJSON decoding JSON decoding is mainly implemented through the Json.parse (JSONSTR) function, because JSON has objects and arrays, so when decoding a JSON string, the structure returned may be a JSON object or a JSON array. JSON encoding JSON encoding is the JSON object or JSON array into JSON string parsing, in order to facilitate the storage and the transmission of data in the network. JSON encoding is mainly implemented through the json.stringify (jsonobj) function.
The 1th line of code above is to create and initialize the JSON object. Note the difference between the following code, the right value of the following code is between single quotes, which means that the following code JSONSTR is a string variable, and the above code, line 1th, the right value is not placed between single quotation marks. Use {...} before and after This means that Jsonobj represents a JSON object.
Similar to line 1th above, the Jsonarray variable for line 2nd represents a JSON array, and its right value is used before and after the [...] This is the difference between a JSON array and a JSON object. Both JSON arrays and JSON object encodings are implemented using the Var jsonstr=json.stringify (jsonobj) statement. The results of the log output are as follows:
As you can see from the log output, the difference between a JSON object and a JSON array and a JSON string
COCOS2D-JS HTTP-based network communication