Last active
June 24, 2020 17:51
-
-
Save RReverser/0c0293589eb25a35105b57e310635b02 to your computer and use it in GitHub Desktop.
Embind + Asyncify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install tip-of-tree Emscripten | |
$ emsdk install tot | |
$ emsdk activate tot | |
# Build with Embind and Asyncify enabled | |
$ emcc --bind -s ASYNCIFY mylib.cpp -o mylib.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include <emscripten/bind.h> | |
using namespace std::string_literals; | |
using emscripten::val; | |
static const val fetch = val::global("fetch"); | |
int main() { | |
val response = fetch("https://httpbin.org/headers"s).await(); | |
if (response["ok"].as<bool>()) { | |
auto text = response.call<val>("text").await().as<std::string>(); | |
std::cout << "Response:" << std::endl << text << std::endl; | |
} else { | |
std::cerr << "Error " << response["status"].as<int>() << ": " | |
<< response["statusText"].as<std::string>() << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment