CocosCreator Getting Started Tutorial: Network Communication

CocosCreator Getting Started Tutorial: Network Communication

Network Communication Overview

When developing an online game, you inevitably have to deal with network communications. There are a few issues to note:

1. For rapid development, the server may use http communication in the early stage and then change to websocket/socket later.

2. Both http and websocket/socket communication exist

3. The communication data format may need to be replaced with JSON or protocol buffer at any time, and additional headers may need to be added.

4. Use local data for simulation in the novice guide, and the request does not need to be sent to the backend.

5. Simple data synchronization after reconnection

Specific implementation

Regarding the first and second questions, when designing the communication module, I will consider abstracting the communication into a unified external interface as much as possible, and create instances through different drivers when the game starts (if only one communication method is used, use a single instance).

public constructor(driver: ConnectDriver) {
        this._driver = driver;
}

First define the general network communication data structure. The requests are divided into two types: data and control, which are used to distinguish heartbeat, login, and reconnection.

/**
 * Request Type */
export enum RequestType {
    DATA = 1,
    CTRL
}
 
/**
 * Network communication data structure */
export interface NetData {
    seq?: number; // Serial number mod: number; // Module cmd: number; // Command path?: string; // Path data?: any; // Data tmpData?: any; // Data processor temporary data status?: number; // Status code type?: RequestType; // Request type}

Then define the common external interface

public open(url: string, port: number, isBinary: boolean, timeout: number, retryCount: number, params: any, cb: (succ: boolean) => void): void {}
 
public close(): number {}
 
public reopen(cb: (succ: boolean) => void): void {}
 
public sendData(data: NetData, params: HttpReq | any, succCb: (data: NetData) => void, failedCb: (code: number, reason: string) => void): void {}
 
public resendNotRecv(): void {}

resendNotRecv is used to send packets that have not received a response after reconnection, and the backend decides whether to process it based on the sequence number.

There is no push processing here. In fact, push is to call broadcast to the listener after receiving the server data. The following defines the listener

/**
 * Network connection event monitoring interface */
export interface ConnectEventListener {
    onOpen(driver: ConnectDriver);
    onClosed(driver: ConnectDriver);
    onError(driver: ConnectDriver, msg: string);
    onSendStart(driver: ConnectDriver);
    onRecvEnd(driver: ConnectDriver);
}
 
/**
 * Network event monitoring interface */
export interface NetEventListener extends ConnectEventListener {
    onPush(driver: ConnectDriver, data: NetData): void;
}

Provide registration and removal interfaces to external parties

public addEventListener(listener: NetEventListener): void {}
 
public removeEventListener(listener: NetEventListener): void {}

For the third problem, we need to define some preprocessors to perform interception and preprocessing before sending data and after receiving data.

/**
 * Network communication data processor interface */
export interface NetDataProcessor {
    processReqData(data: any): any;
    processRespData(data: any): any;
}

Provides add and remove interfaces to the outside world, so that the communication data format can be flexibly defined

public addDataProcessor(processor: NetDataProcessor): void {}
 
public removeDataProcessor(processor: NetDataProcessor): void {}

For the fourth problem, we also add a special interceptor to process the request before it is sent. If the interceptor can handle it, it will no longer be sent to the backend.

/**
 * Simulate server interface */
export interface SimServer {
    handleRequest(data: NetData): NetData;
}

Provide external settings interface

public setSimServer(server: SimServer): void {}

For the fifth question, all data requests have serial numbers, the requests are recorded, and the unresponsive request packets are placed in a queue. After reconnection, these packets are resent to the backend for processing.

public resendNotRecv(): void {}

The above is the detailed content of the network communication in the introductory tutorial of CocosCreator. For more information about CocosCreator network communication, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Cocos2d-x 3.x Getting Started Tutorial (Part 2): Node Class
  • Cocos2d-x 3.x Getting Started Tutorial (I): Basic Concepts
  • Cocos2d-x Getting Started Tutorial (Detailed Examples and Explanations)
  • Detailed explanation of making shooting games with CocosCreator
  • How to draw a cool radar chart in CocosCreator
  • Detailed explanation of CocosCreator MVC architecture
  • Detailed explanation of CocosCreator message distribution mechanism
  • How to create WeChat games with CocosCreator
  • Detailed explanation of how CocosCreator system events are generated and triggered
  • How to use a game controller in CocosCreator
  • Detailed explanation of CocosCreator Huarongdao digital puzzle
  • CocosCreator Getting Started Tutorial: Making Your First Game with TS

<<:  Solution to Chinese garbled characters when operating MySQL database in CMD

>>:  One question to understand multiple parameters of sort command in Linux

Recommend

Native js canvas to achieve a simple snake

This article shares the specific code of js canva...

3 methods to restore table structure from frm file in mysql [recommended]

When mysql is running normally, it is not difficu...

Detailed explanation of Tomcat's Server Options

1. Configuration By default, the first two are no...

Implementation of postcss-pxtorem mobile adaptation

Execute the command to install the plugin postcss...

CSS sample code with search navigation bar

This article shows you how to use CSS to create a...

A brief introduction to the general process of web front-end web development

I see many novice students doing front-end develop...

Tutorial on using Docker Compose to build Confluence

This article uses the "Attribution 4.0 Inter...

Detailed explanation of slave_exec_mode parameter in MySQL

Today I accidentally saw the parameter slave_exec...

jQuery achieves breathing carousel effect

This article shares the specific code of jQuery t...

Tutorial on building an FTP server in Ubuntu 16.04

Ubuntu 16.04 builds FTP server Install ftp Instal...

Use xshell to connect to the Linux server

Benefits of using xshell to connect to Linux We c...

HTML hyperlink a tag_Powernode Java Academy

Anyone who has studied or used HTML should be fam...

Test and solution for MySQL's large memory usage and high CPU usage

After the changes: innodb_buffer_pool_size=576M -...

Several specific methods of Mysql space cleaning

Table of contents Preface 1. Check the file disk ...