Skip to content

Instantly share code, notes, and snippets.

@arielm
Created November 22, 2021 12:20
Show Gist options
  • Save arielm/11567c883d1f0662eaec56b1cb3e0b0e to your computer and use it in GitHub Desktop.
Save arielm/11567c883d1f0662eaec56b1cb3e0b0e to your computer and use it in GitHub Desktop.
Test to load a file asynchronously with emscripten
#include <emscripten/emscripten.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int pending_count = 0;
void onload(unsigned handle, void* arg, void* data, unsigned length) {
const char *text = (const char*)data;
printf("RECEIVED:\n[%s]\n[%i]\n", text, length);
pending_count--;
}
void onerror(unsigned handle, void* arg, int status, const char* status_text) {
printf("ERROR: [%i][%s]\n", status, status_text);
pending_count--;
}
void onprogress(unsigned handle, void* arg, int loaded, int total) {
printf("PROGRESS: %i/%i\n", loaded, total);
}
void create_request(const char* target, const char* method) {
emscripten_async_wget2_data(target, method, "", NULL, true, onload, onerror, onprogress);
};
void wait() {
if (pending_count <= 0) {
emscripten_cancel_main_loop();
} else {
printf("Not ready (%i requests pending)\n", pending_count);
}
}
int main() {
create_request("test_other.py", "GET");
emscripten_set_main_loop(wait, 0, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment