Compare commits
No commits in common. "6b15c9e20c97814191426902448866182ae54c18" and "98d0af014d02f6d9d6513d393a9167fad957a9fd" have entirely different histories.
6b15c9e20c
...
98d0af014d
3 changed files with 3 additions and 71 deletions
|
|
@ -685,33 +685,13 @@ let env = {
|
||||||
const response = await fetch(urlStr);
|
const response = await fetch(urlStr);
|
||||||
const result = await response.text();
|
const result = await response.text();
|
||||||
|
|
||||||
const ptr = writeStringToWasm(result);
|
console.log(result);
|
||||||
|
|
||||||
const { ExecuteFetchHandler} = window.crafter_webbuild_wasi.instance.exports;
|
|
||||||
ExecuteFetchHandler(handlerID, ptr);
|
|
||||||
WasmFree(ptr);
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
fetchWithBody: async function(url, urlLenght, body, bodyLength, handlerID) {
|
|
||||||
try {
|
|
||||||
const urlStr = decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, url, urlLenght));
|
|
||||||
const bodyStr = decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, body, bodyLength));
|
|
||||||
|
|
||||||
const response = await fetch(urlStr, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: bodyStr
|
|
||||||
});
|
|
||||||
const result = await response.text();
|
|
||||||
|
|
||||||
const ptr = writeStringToWasm(result);
|
const ptr = writeStringToWasm(result);
|
||||||
|
console.log(ptr);
|
||||||
|
|
||||||
const { ExecuteFetchHandler} = window.crafter_webbuild_wasi.instance.exports;
|
const { ExecuteFetchHandler} = window.crafter_webbuild_wasi.instance.exports;
|
||||||
|
console.log("callback");
|
||||||
ExecuteFetchHandler(handlerID, ptr);
|
ExecuteFetchHandler(handlerID, ptr);
|
||||||
WasmFree(ptr);
|
WasmFree(ptr);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,42 +0,0 @@
|
||||||
# FetchExample
|
|
||||||
|
|
||||||
This sample demonstrates how to use the Fetch API in Crafter.CppDOM to make HTTP requests.
|
|
||||||
|
|
||||||
## Features
|
|
||||||
|
|
||||||
- Shows how to perform HTTP GET requests
|
|
||||||
- Demonstrates asynchronous response handling
|
|
||||||
- Illustrates how to update the DOM with fetched data
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
import Crafter.CppDOM;
|
|
||||||
import std;
|
|
||||||
using namespace Crafter::CppDOMBindings;
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
void* body = GetElementById("body");
|
|
||||||
SetInnerHTML(body, "<h1>Fetch Example</h1><p>Testing HTTP requests...</p>");
|
|
||||||
|
|
||||||
Fetch("https://httpbin.org/get", [body](std::string result){
|
|
||||||
if (!result.empty()) {
|
|
||||||
SetInnerHTML(body, "<h1>Fetch Example</h1><p>Response: " + result + "</p>");
|
|
||||||
} else {
|
|
||||||
SetInnerHTML(body, "<h1>Fetch Example</h1><p>Failed to fetch data</p>");
|
|
||||||
}
|
|
||||||
FreeJs(body);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Building and Running
|
|
||||||
|
|
||||||
```bash
|
|
||||||
crafter-build build executable
|
|
||||||
run.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
Then navigate to `http://localhost:8080/` in your browser.
|
|
||||||
|
|
||||||
If caddy is not installed, you can use your favorite static file server instead.
|
|
||||||
|
|
@ -217,15 +217,9 @@ extern "C" {
|
||||||
|
|
||||||
export namespace Crafter::CppDOMBindings {
|
export namespace Crafter::CppDOMBindings {
|
||||||
__attribute__((import_module("env"), import_name("fetch"))) void Fetch(const char* url, std::int32_t urlLenght, std::int32_t handlerID);
|
__attribute__((import_module("env"), import_name("fetch"))) void Fetch(const char* url, std::int32_t urlLenght, std::int32_t handlerID);
|
||||||
__attribute__((import_module("env"), import_name("fetchWithBody"))) void FetchWithBody(const char* url, std::int32_t urlLenght, const char* body, std::int32_t bodyLength, std::int32_t handlerID);
|
|
||||||
void Fetch(const std::string_view url, std::function<void(std::string)> callback) {
|
void Fetch(const std::string_view url, std::function<void(std::string)> callback) {
|
||||||
std::int32_t id = fetchHandlerMaxId++;
|
std::int32_t id = fetchHandlerMaxId++;
|
||||||
CppDOMBindings::fetchHandlers->insert({id, callback});
|
CppDOMBindings::fetchHandlers->insert({id, callback});
|
||||||
Fetch(url.data(), url.size(), id);
|
Fetch(url.data(), url.size(), id);
|
||||||
}
|
}
|
||||||
void Fetch(const std::string_view url, const std::string_view body, std::function<void(std::string)> callback) {
|
|
||||||
std::int32_t id = fetchHandlerMaxId++;
|
|
||||||
CppDOMBindings::fetchHandlers->insert({id, callback});
|
|
||||||
FetchWithBody(url.data(), url.size(), body.data(), body.size(), id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue