Skip to content

Commit

Permalink
Move WS lib import into ws_hal
Browse files Browse the repository at this point in the history
  • Loading branch information
K0rdan committed Feb 19, 2024
1 parent e4cf2e2 commit 01d3fce
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion engine/HAL/BROWSER/hal_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@
RANLIB=cheerp_bin_path + "/llvm-ar s")

# Replace the output filename with the appropriate extension
e.Replace(PROGNAME="program.js")
e.Replace(PROGNAME="program.mjs")
4 changes: 0 additions & 4 deletions examples/projects/browser/led/src/main.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import program from '../.pio/build/browser/program.mjs';

if (globalThis.WebSocket === undefined) {
globalThis.WebSocket = await import('ws').then((mod) => mod.WebSocket);
}

program()
.then(({ Luos_Init, Led_Init, Luos_Loop, Led_Loop, Ws_Init, Ws_Loop }) => {
Luos_Init();
Expand Down
29 changes: 26 additions & 3 deletions network/ws_network/HAL/BROWSER/ws_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ extern "C"

using namespace client;

namespace [[cheerp::genericjs]] client
{
Promise *import(const String &path);
}

/*******************************************************************************
* Definitions
******************************************************************************/
Expand Down Expand Up @@ -63,9 +68,7 @@ void WsHAL_ReceptionWeb(const std::vector<uint8_t> &data)
clientWebsocket->send(arrayBufferView);
}


[[cheerp::genericjs]] void WsHAL_InitWeb()
{
[[cheerp::genericjs]] void WsHAL_AddWsEventHandlers() {
clientWebsocket = new WebSocket(s_url);
clientWebsocket->addEventListener(
"open",
Expand All @@ -87,6 +90,26 @@ void WsHAL_ReceptionWeb(const std::vector<uint8_t> &data)
}));
}

[[cheerp::genericjs]] void WsHAL_InitWeb()
{
bool isBrowser;
__asm__("typeof %1 !== 'undefined'" : "=r"(isBrowser) : "r"(&client::window));
if (isBrowser) {
clientWebsocket = new WebSocket(s_url);
WsHAL_AddWsEventHandlers();
}
else
{
client::import("ws")
->then(cheerp::Callback([](client::Object *lib) {
__asm__("globalThis.WebSocket=%0.WebSocket" ::"r"(lib));
}))
->then(cheerp::Callback([]() {
WsHAL_AddWsEventHandlers();
}));
}
}

/////////////////////////Luos Library Needed function///////////////////////////

/******************************************************************************
Expand Down

0 comments on commit 01d3fce

Please sign in to comment.