Emscripten Fetch始终返回0

问题描述 投票:1回答:1

我遵循Emscripten附带的Synchronous Fetch示例,如下所示;

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);
}

它始终从获取状态返回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

但是当我用async测试并读取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)

c++ xmlhttprequest emscripten
1个回答
1
投票

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

同步Emscripten Fetch操作受到许多限制,具体取决于使用的Emscripten构建模式(链接器标志):

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

-proxy-to-worker:对仅执行XHR但不与IndexedDB交互的提取允许同步提取操作。

-s USE_PTHREADS = 1:同步提取操作在pthread上可用,但在主线程上不可用。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.