logo
On this page

Run sample code


ZEGOCLOUD provides sample server code for Node.js and sample client code for iOS. You can create an application and experience all features of the mini-games with activated permissions by running the two scripts of sample code. You can also learn how to implement a mini-game service by reading the sample code.

This topic involves the following sections:

  1. Run the sample server code for Node.js
  2. Run the sample client code for iOS
Warning
  • The preceding sample code is provided only for your reference. ZEGOCLOUD is not responsible for the maintenance of the sample code.
  • The preceding sample code does not pass a rigorous test. If you want to use the sample code in a production environment, perform a rigorous test before release to prevent losses.

Run the sample server code for Node.js

Warning

The sample server code cannot be used in a production environment for data security. ZEGOCLOUD is not liable for any risks or losses that are caused if you use the sample server code in a production environment.

Prepare the environment

Before you run the sample server code for Node.js, make sure that the development environment meets the following requirements:

  • Node.js is installed.
  • A computer that runs Windows or macOS is connected to the internet.

Prerequisites

  • A project is created in the ZEGOCLOUD console. The values of the AppID and ServerSecret parameters of the project are obtained. For more information, see How to view project information.
  • The mini-game service is activated by contacting ZEGOCLOUD business staff.
  • The sample server code is obtained. For more information, see Download.

Directory structure of the sample code

The following section describes the directory structure of the sample server code. All file paths involved in this topic are based on the directory structure.

Untitled
ZegoMiniGameServerDemo
├── index.js        # The main file.
├── package.json    # The configuration file.
└── utils
  ├── config.js    # Enter the values of the `AppID` and `ServerSecret` parameters that you obtain in the ZEGOCLOUD console.
  ├── gameToken.js # The token generated by the server. 
  └── getToken04.js 
1
Copied!

Run the sample code

1
Decompress the `ZegoMiniGameServerDemo.zip` package of the sample server code to obtain the `ZegoMiniGameServerDemo` folder.
2
Modify the `ZegoMiniGameServerDemo/utils/config.js` file by specifying the `appID` parameter in the red-framed part as shown in the following figure.

Fill in the appId requested from the ZEGOCLOUD console

Fill in the serverSecret requested from the ZEGOCLOUD console

3
In the `ZegoMiniGameServerDemo/utils/config.js` file, specify the `serverSecret` parameter.

Fill in the appId requested from the ZEGOCLOUD console

Fill in the serverSecret requested from the ZEGOCLOUD console

Warning

The https://mini-game-api.zego.im host address provided in this file is the endpoint of the mini-game server and cannot be modified.

4
Go to the `ZegoMiniGameServerDemo` folder and run the following code on the terminal to start the mini-game server.
Untitled

# Install dependencies.

npm i  

node index.js
1
Copied!
5
Obtain the server address, which can be used as the backend address in the sample client code

The following figure shows the output after the sample server code is run.

Run the sample client code for iOS

Prepare the environment

Before you run the sample client code for iOS, make sure that the development environment meets the following requirements:

  • Xcode 15.0 or later is installed.
  • iOS 11.0 or later is installed. We recommend that you use a physical device.
  • The iOS device is connected to the internet. We recommend that you use a physical device.
  • CocoaPods is installed. For more information about the installation method and common issues of CocoaPods, visit the official website of CocoaPods (https://cocoapods.org/).

Prerequisites

Directory structure of the sample code

The following section describes the directory structure of sample code for iOS. All file paths involved in this section are based on the directory structure.

Untitled
ZegoMiniGameEngineDemo/
├── Podfile
├── Podfile.lock
├── ZegoMiniGameEngineDemo
├── ZegoMiniGameEngineDemo.xcodeproj
└── ZegoMiniGameEngineDemo.xcworkspace
1
Copied!

Run the sample code

1
Open the App Store and search Xcode. Then, download and install Xcode.
2
Decompress the iOS client example source code package to get the folder 'ZegoMiniGameEngineDemoSrc-iOS, and navigate to the folder using the terminal.
3
Execute the `pod repo update` command to update the local index and ensure the latest version of the SDK can be installed.
4
Execute the `pod install` command to install the SDK.
5
Open Xcode, select the menu at the top left 'File > Open...', and find and open the 'ZegoMiniGameEngineDemo.xcworkspace' file in the extracted example source code folder.
6
Log in as a developer.
  1. Open Xcode. In the upper-left corner in the top navigation bar, choose Xcode > Preferences.
  1. Click the Accounts tab. In the lower-left corner, click the plus icon (+). In the dialog that appears, select Apple ID and click Continue.
  1. Enter your Apple ID and password to log in.
7
Modify the bundle identifier and the developer certificate.

Open Xcode. In the left-side navigation pane, click the ZegoMiniGameEngineDemo project. Click the Signing & Capabilities tab and modify the Team and Bundle Identifier parameters as needed.

8
Modify the file '/ZegoMiniGameEngineDemo/KeyCenter.m', fill in the `appID` necessary for SDK initialization, and update the `hostUrl` address to the one obtained after successfully running the sample server code (as described in the [Prerequisites](#16586#2_2) section of this document).
9
Connect the iOS device to your computer and click 'Any iOS Device' in the upper part of Xcode. In the dialog that appears, select the iOS device or the emulator.
10
In the upper-left corner of Xcode, click 'Build' to compile and run the sample code. Then, you can experience the mini-game.

Previous

Experience App

Next

Integrate the SDK