Create server request backbone and functions for talking to save file server.

This commit is contained in:
2024-01-02 04:45:44 -06:00
parent 3e5e78a606
commit 2a900fa06e
11 changed files with 115 additions and 21 deletions
+28 -17
View File
@@ -473,6 +473,7 @@ namespace _gfs = std::filesystem;
#if defined(__EMSCRIPTEN__)
#define OLC_PLATFORM_EMSCRIPTEN
#include <emscripten/fetch.h>
#include <emscripten/emscripten.h>
#endif
#endif
#endif
@@ -996,6 +997,7 @@ namespace olc
virtual void OnTextEntryComplete(const std::string& sText);
// Called when a console command is executed
virtual bool OnConsoleCommand(const std::string& sCommand);
virtual void OnRequestCompleted(const std::string_view receivedData)const;
virtual olc::rcode SendRequest(std::string_view url,std::string_view data);
@@ -1242,6 +1244,8 @@ namespace olc
public:
static std::string responseData;
// Experimental Lightweight 3D Routines ================
#ifdef OLC_ENABLE_EXPERIMENTAL
// Set Manual View Matrix
@@ -2044,6 +2048,8 @@ namespace olc
return o;
};
std::string PixelGameEngine::responseData;
// O------------------------------------------------------------------------------O
// | olc::PixelGameEngine IMPLEMENTATION |
// O------------------------------------------------------------------------------O
@@ -4393,6 +4399,8 @@ namespace olc
void PixelGameEngine::OnTextEntryComplete(const std::string& sText) { UNUSED(sText); }
bool PixelGameEngine::OnConsoleCommand(const std::string& sCommand) { UNUSED(sCommand); return false; }
void PixelGameEngine::OnRequestCompleted(const std::string_view receivedData)const{}
olc::rcode PixelGameEngine::SendRequest(std::string_view url,std::string_view data){platform->SendRequest(url,data);return olc::rcode::OK;};
@@ -7206,23 +7214,10 @@ namespace olc
}
virtual olc::rcode SendRequest(std::string_view url,std::string_view data)override{
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
attr.attributes=EMSCRIPTEN_FETCH_LOAD_TO_MEMORY|EMSCRIPTEN_FETCH_SYNCHRONOUS|EMSCRIPTEN_FETCH_REPLACE;
emscripten_fetch_t*fetch=emscripten_fetch(&attr,std::string(url).c_str()); // Blocks here until the operation is complete.
if(fetch->status==200){
printf("Finished downloading %llu bytes from URL %s.\n", fetch->numBytes, fetch->url);
// The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1];
}else{
printf("Downloading %s failed, HTTP failure status code: %d.\n",fetch->url,fetch->status);
}
emscripten_fetch_close(fetch);
for(int i=0;i<fetch->numBytes;i++){
std::cout<<fetch->data[i];
}
std::cout<<std::endl;
EM_ASM({
requestResp="";
fetch(UTF8ToString($0)).then((resp)=>resp.text()).then((data)=>{requestResp=data});
},std::string(url).c_str());
return olc::rcode::OK;
}
@@ -7350,6 +7345,22 @@ namespace olc
static void MainLoop()
{
olc::Platform::ptrPGE->olc_CoreUpdate();
char*ptr=(char*)EM_ASM_PTR({
if(requestResp!==""){
console.log("Request received: "+requestResp);
var newPtr=stringToNewUTF8(requestResp);
requestResp="";
return newPtr;
}
return 0;
});
if(ptr!=NULL){
PixelGameEngine::responseData=std::string(ptr);
ptrPGE->OnRequestCompleted(PixelGameEngine::responseData);
delete ptr;
}
if (!ptrPGE->olc_IsRunning())
{
if (ptrPGE->OnUserDestroy())