Standard network interface

In Cocos Creator, we support the most widely used standard network interface on the Web platform:

Of course, browsers on the Web platform support these two interfaces originally. The reason why we say Cocos Creator supports it is because when we release the native version, the user can operate it using these two network interface codes which follows the principle of "one set of code for multiple platforms operation" which Cocos honors.

How to use

  1. XMLHttpRequest

Simple example:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function ()
   if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400))
      var response = xhr.responseText;
};"GET", url, true);

Developers can use new XMLHttpRequest() directly or use cc.loader.getXMLHttpRequest() to create a connecting object. The effect of these two are the same.

For the standard file of XMLHttpRequest, please refer to MDN Chinese file

  1. WebSocket

Simple example:

ws = new WebSocket("ws://");
ws.onopen = function (event) {
    console.log("Send Text WS was opened.");
ws.onmessage = function (event) {
    console.log("response text msg: " +;
ws.onerror = function (event) {
    console.log("Send Text fired an error");
ws.onclose = function (event) {
    console.log("WebSocket instance closed.");

setTimeout(function () {
    if (ws.readyState === WebSocket.OPEN) {
        ws.send("Hello WebSocket, I'm a text message.");
    else {
        console.log("WebSocket instance wasn't ready...");
}, 3);

For the standard file of WebSocket, please refer toMDN Chinese file


Beyond these, SocketIO provides packaging based on the WebSocket API which can be used on the Node.js server. If this library is needed, developers can reference SocketIO on their own.

Reference SocketIO in script:

  1. Download SocketIO:Download link
  2. Drag the downloaded file into the route you would like to save in explorer
  3. Reference SocketIO in the component script:
// Judge whether it is a native environment or not, if it is then it can not be referenced because native provides native SocketIO to achieve
if (cc.sys.isNative) {
    // io variable in native environment has not been defined, the exported variable is actually SocketIO = SocketIO;
else {
    // use relative path, do not need .js as postfix
  1. To use SocketIO in the component you can go to SocketIO official website for API and files