Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure WeChat Mini Programs Mini Game multithreaded Worker

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces WeChat Mini Programs Mini Game multithreaded Worker configuration related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone after reading this WeChat Mini Programs Mini Game multithreaded Worker configuration article will have a harvest, let's take a look at it.

Multithreaded Worker

For the game, each frame 16ms is extremely valuable, if there are some tasks that can be handled asynchronously, you can run it in Worker, and then return the results to the main thread when the run is over. Worker runs in a separate global context and thread, and the method of the main thread cannot be called directly, and Worker does not have the ability to render. In the data transmission between Worker and the main thread, both parties use postMessage to send data and onMessage to receive data. The transmitted data is not directly shared, but copied.

Steps

1. Configure Worker information

In game.json, you can configure the directory where the Worker code is placed, and the code in the directory will be packaged into a file:

Example configuration:

{

"workers": "workers"

}

two。 Add Worker code file

According to the configuration in step 1, create the following two new entry files in the code directory:

Workers/request/index.js

Workers/request/utils.js

Workers/response/index.js

After being added, the directory structure is as follows:

├── game.js

├── game.json

├── project.config.json

└── workers

├── request

│ ├── index.js

│ └── utils.js

└── response

└── index.js

3. Write Worker code

Write Worker response code in workers/request/index.js

Const utils = require ('. / utils')

Worker.onMessage (function (res) {

Console.log (res)

})

4. Initialize Worker in the main thread

Initialize Worker in the code game.js of the main thread

Const worker = wx.createWorker ('workers/request/index.js') / / File name specifies the entry file path of worker, absolute path

5. The main thread sends a message to Worker

Worker.postMessage ({

Msg: 'hello worker'

})

For other APIs of worker object, please see worker API description.

Tips

The maximum number of concurrency of Worker is limited to 1. Please end the current Worker with Worker.terminate before creating the next one.

The code in Worker can only require files within the specified Worker path, and cannot refer to other paths.

The entry file of Worker is specified by wx.createWorker, and developers can specify the entry file of Worker dynamically.

Wx series API is not supported in Worker

Sending messages between Workers is not supported

This is the end of the article on "how to configure WeChat Mini Programs Mini Game multithreaded Worker". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "WeChat Mini Programs Mini Game multithreaded Worker configuration". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report