add network.post

This commit is contained in:
MihailRis
2025-01-08 17:02:07 +03:00
parent 5b6ad81eba
commit 96b904e61f
3 changed files with 91 additions and 10 deletions
@@ -2,6 +2,7 @@
#include "engine/Engine.hpp"
#include "network/Network.hpp"
#include "coders/json.hpp"
using namespace scripting;
@@ -36,6 +37,25 @@ static int l_get_binary(lua::State* L) {
return 0;
}
static int l_post(lua::State* L) {
std::string url(lua::require_lstring(L, 1));
auto data = lua::tovalue(L, 2);
lua::pushvalue(L, 3);
auto onResponse = lua::create_lambda_nothrow(L);
auto string = json::stringify(data, false);
engine->getNetwork().post(url, string, [onResponse](std::vector<char> bytes) {
auto buffer = std::make_shared<util::Buffer<ubyte>>(
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
);
engine->postRunnable([=]() {
onResponse({std::string(bytes.data(), bytes.size())});
});
});
return 0;
}
static int l_connect(lua::State* L) {
std::string address = lua::require_string(L, 1);
int port = lua::tointeger(L, 2);
@@ -200,6 +220,7 @@ static int l_get_total_download(lua::State* L) {
const luaL_Reg networklib[] = {
{"get", lua::wrap<l_get>},
{"get_binary", lua::wrap<l_get_binary>},
{"post", lua::wrap<l_post>},
{"get_total_upload", lua::wrap<l_get_total_upload>},
{"get_total_download", lua::wrap<l_get_total_download>},
{"__open", lua::wrap<l_open>},