[adapters] [extension] 增加对多线程 HTTP 的支持,修复创建多线程扩展错误的问题

This commit is contained in:
SmallMain
2024-11-14 20:03:04 +08:00
parent 22941a3c1f
commit 02eb90b78e
21 changed files with 404 additions and 67 deletions

View File

@@ -9,3 +9,8 @@ if (globalThis.CC_WORKER_AUDIO_SYSTEM) {
const audio = require("./audio-worker.js");
registerHandler("audio", audio);
}
if (globalThis.CC_WORKER_HTTP_REQUEST) {
const http = require("./http-worker.js");
registerHandler("http", http);
}

View File

@@ -0,0 +1,56 @@
const { main } = require("./ipc-worker.js");
const methodMap = {
1: 'GET',
2: 'POST',
3: 'HEAD',
4: 'PUT',
5: 'DELETE',
6: 'CONNECT',
7: 'OPTIONS',
8: 'TRACE',
9: 'PATCH',
};
var http_worker = {
tasks: {},
request(id, data, url, method, header, responseType, timeout, callback) {
const task = worker.request({
data,
url: url,
method: methodMap[method],
header: header,
dataType: 'other',
responseType: responseType === 0 ? 'arraybuffer' : 'text',
timeout: typeof timeout === "number" ? timeout : undefined,
success: ({ data, statusCode, header }) => {
delete this.tasks[id];
callback(
true,
data,
statusCode,
header,
);
},
fail: ({ errMsg }) => {
delete this.tasks[id];
callback(false, errMsg);
},
useHighPerformanceMode: true,
enableHttp2: true,
enableQuic: true,
});
this.tasks[id] = task;
},
abort(id) {
const task = this.tasks[id];
if (task) {
task.abort();
delete this.tasks[id];
}
},
};
module.exports = http_worker;

View File

@@ -146,6 +146,7 @@ function _initFromWorker(id, meta) {
CC_WORKER_ASSET_PIPELINE,
CC_WORKER_AUDIO_SYSTEM,
CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL,
CC_WORKER_HTTP_REQUEST,
] = meta;
for (const wrapper of wrappers) {
@@ -162,6 +163,7 @@ function _initFromWorker(id, meta) {
globalThis.CC_WORKER_ASSET_PIPELINE = CC_WORKER_ASSET_PIPELINE;
globalThis.CC_WORKER_AUDIO_SYSTEM = CC_WORKER_AUDIO_SYSTEM;
globalThis.CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL = CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL;
globalThis.CC_WORKER_HTTP_REQUEST = CC_WORKER_HTTP_REQUEST;
_inited = true;
if (_initCallback) _initCallback();

View File

@@ -18,5 +18,8 @@ globalThis.CC_WORKER_ASSET_PIPELINE = null;
// 是否启用 Worker 驱动音频系统
globalThis.CC_WORKER_AUDIO_SYSTEM = null;
// 是否启用 Worker 驱动 HTTP 请求
globalThis.CC_WORKER_HTTP_REQUEST = null;
// Worker 音频系统同步音频属性的间隔时间(单位:毫秒)
globalThis.CC_WORKER_AUDIO_SYSTEM_SYNC_INTERVAL = null;