I call emsripten_fetch from a member function of my class (SwisVtkRadarWindow). I assign the reference of SwisVtkRadarWindow to the fetch attribute called “userData” by using the “this” keyword.
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, “GET”);
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
attr.userData = this;
attr.onsuccess = downloadSucceeded;
emscripten_fetch(&attr, path.c_str());
Then in the downloadSucceeded callback I use a static cast to get my class from userData and process the data.
void downloadSucceeded(emscripten_fetch_t* fetch) {
while (fetch->readyState != 4)
{
std::cout << “Waiting For Download To Complete.” << std::endl;
}std::cout << "Finished downloading " << fetch->numBytes << " bytes, and " << fetch->totalBytes << " total bytes from URL " << fetch->url << std::endl;
// The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1];
std::vector data(fetch->numBytes, 0);
memcpy(data.data(), fetch->data, fetch->numBytes);
emscripten_fetch_close(fetch); // Free data associated with the fetch.auto radwin = static_cast<SwisVtkRadarWindow*>(fetch->userData);
radwin->DownloadSuccess(data);}
Regarding the Emscripten compiler flags “–proxy-to-worker + -s USE_PTHREADS=1”. When I tried this I was getting compile errors saying I can’t use --proxy-to-worker flags with USE_PTHREADS=1, which conflicts with the documentation.