emscripten获取始终返回0

Emscripten Fetch always return 0

本文关键字:返回 获取 emscripten      更新时间:2023-10-16

我遵循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 %dn", fetch->status);
}

它总是从获取状态返回0

i用

编译
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 ]: %sn", 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.
}

我的获取同步代码有什么问题?一切都与" example_synchronous_fetch.cpp" example

完全相同

我在Windows10上运行。Emscripten 1.38.29。使用Microsoft Edge无需服务器即可直接浏览文件(双击Hello.html)

同步fetch有一些额外的限制,似乎您的构建标志不启用同步fetch

同步emscripten提取操作受到许多限制,具体取决于哪种emscripten构建模式(链接标志)使用:

无标志:只有异步提取操作。

–proxy-to-worker:仅进行XHR但不与indexedDB相互作用的获取允许同步提取操作。

-s use_pthreads = 1:同步提取操作可在pthreads上获得,但在主线程上不可用。

- proxy-to-worker -s use_pthreads = 1:同步提取操作在主线程和pthreads上都可用。

https://emscripten.org/docs/api_reference/fetch.html#synchronous-fetches