Emscripten Fetch 总是 return 0
Emscripten Fetch always return 0
我遵循 Emscripten 附带的同步获取示例,如下所示;
void main()
{
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS;
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "https://ichef.bbci.co.uk/news/660/cpsprodpb/E9DF/production/_96317895_gettyimages-164067218.jpg");
printf("Fetch finished with status %d\n", fetch->status);
}
它总是returns 0 来自获取状态
我用
编译
FLAGS += -std=c++17 -stdlib=libc++ -O3
FLAGS += -s WASM=1 -s USE_WEBGL2=1 -s FULL_ES3=1
FLAGS += -s ALLOW_MEMORY_GROWTH=1
FLAGS += -o hello.html
FLAGS += -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']"
FLAGS += --no-heap-copy
FLAGS += -s FETCH=1
然而,当我使用异步测试并读取 OnSuccess 函数中的数据时。数据打印正确
static
void ondownload_success(emscripten_fetch_t *fetch)
{
printf("[ download ][ OK ] %llu bytes [ URL ]: %s\n", fetch->numBytes, fetch->url);
printf("%c %c %c", fetch->data[0], fetch->data[3], fetch->data[2] );
emscripten_fetch_close(fetch); // Free data associated with the fetch.
}
我的 Fetch 同步代码有什么问题?一切都与 "example_synchronous_fetch.cpp" 示例
完全相同
我 运行 在 Windows10。 Emscripten 1.38.29。使用Microsoft Edge 无需服务器直接浏览文件(双击hello.html)
同步 fetch
有一些额外的限制,您的构建标志似乎没有启用同步 fetch
:
Synchronous Emscripten Fetch operations are subject to a number of
restrictions, depending on which Emscripten build mode (linker flags)
is used:
No flags: Only asynchronous Fetch operations are available.
–proxy-to-worker: Synchronous Fetch operations are allowed for fetches that only do an XHR but do not interact with IndexedDB.
-s USE_PTHREADS=1: Synchronous Fetch operations are available on pthreads, but not on the main thread.
–proxy-to-worker + -s USE_PTHREADS=1: Synchronous Fetch operations are available both on the main thread and pthreads.
https://emscripten.org/docs/api_reference/fetch.html#synchronous-fetches
我遵循 Emscripten 附带的同步获取示例,如下所示;
void main()
{
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS;
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "https://ichef.bbci.co.uk/news/660/cpsprodpb/E9DF/production/_96317895_gettyimages-164067218.jpg");
printf("Fetch finished with status %d\n", fetch->status);
}
它总是returns 0 来自获取状态
我用
编译FLAGS += -std=c++17 -stdlib=libc++ -O3
FLAGS += -s WASM=1 -s USE_WEBGL2=1 -s FULL_ES3=1
FLAGS += -s ALLOW_MEMORY_GROWTH=1
FLAGS += -o hello.html
FLAGS += -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']"
FLAGS += --no-heap-copy
FLAGS += -s FETCH=1
然而,当我使用异步测试并读取 OnSuccess 函数中的数据时。数据打印正确
static
void ondownload_success(emscripten_fetch_t *fetch)
{
printf("[ download ][ OK ] %llu bytes [ URL ]: %s\n", fetch->numBytes, fetch->url);
printf("%c %c %c", fetch->data[0], fetch->data[3], fetch->data[2] );
emscripten_fetch_close(fetch); // Free data associated with the fetch.
}
我的 Fetch 同步代码有什么问题?一切都与 "example_synchronous_fetch.cpp" 示例
完全相同我 运行 在 Windows10。 Emscripten 1.38.29。使用Microsoft Edge 无需服务器直接浏览文件(双击hello.html)
同步 fetch
有一些额外的限制,您的构建标志似乎没有启用同步 fetch
:
Synchronous Emscripten Fetch operations are subject to a number of restrictions, depending on which Emscripten build mode (linker flags) is used:
No flags: Only asynchronous Fetch operations are available.
–proxy-to-worker: Synchronous Fetch operations are allowed for fetches that only do an XHR but do not interact with IndexedDB.
-s USE_PTHREADS=1: Synchronous Fetch operations are available on pthreads, but not on the main thread.
–proxy-to-worker + -s USE_PTHREADS=1: Synchronous Fetch operations are available both on the main thread and pthreads.
https://emscripten.org/docs/api_reference/fetch.html#synchronous-fetches