BernardoGiordano / nx-spa

GNU General Public License v3.0
7 stars 1 forks source link

JS <-> C++ communication issues #2

Open Slluxx opened 1 year ago

Slluxx commented 1 year ago

To make anything usable, it is nessesary that the frontend can communicate with the backend. This can be done in multiple ways.

Last year i tried to do something very similar with mixed results but i knew its possible. Having a working communication example would be huge and lower the homebrew development barrier a lot.

BernardoGiordano commented 1 year ago

Frontend is already capable to communicate with the backend. There's a simple PoC HTTP exchange already setup in the Angular demo.

The main issues to making really anything usable are:

Slluxx commented 1 year ago

Ah nice, i didnt spot that. The approach you chose, delivering either html or something else based on the endpoint on the same thread, is in my opinion the least favourable though. Its quite messy in comparison to a libnx-native solution (or a websocket server) on another thread. It would keep the webserver and process intense operations seperated and allow for independend bi-directional communication.

For example with the current approach, its not possible to send data to the browser without the browser always trying to fetch it first. Monitoring anything would be much more simple and the browser/js actively reacting to changes would be huge.

being able to still communicate through client-to-server HTTP requests if the console is in airplane mode;

I would actually neglect that. No ones needs to have airplane mode on. And its almost self explanatory that a browser would not work in airplane mode. You could also display a simple message if airplane mode is on until you find a way to circumvent this issue.

avoid making the thread unresponsive after the console has gone in sleep mode while using an app that relies on the same technology as nx-spa

Dont let the console go to sleep. appletSetAutoSleepDisabled(true); will wakelock the console until you run the function with false or the application is exited in any way.


Looks like mongoose can do websockets very easily (at least on the latest release).


This was easier than i thought, even though the current version of mongoose has way cleaner code to handle websockets.

IMG_20231102_032517

Slluxx commented 1 year ago

I have created a few classes that dynamically manage threads based on tasks one can set up. The setup basically works like this (messy testing code)

  task_charger.func = [](Task &task) {

    PsmChargerType current;
    psmGetChargerType(&current);
    PsmChargerType old;
    psmGetChargerType(&old);

    while (!task.threadForceExit.load()) {
        psmGetChargerType(&current);
        if (current != old){
          broadcast(nc, mg_mk_str("charger type changed"));
        }
        old = current;
       svcSleepThread(1000000000ull); // 1s
    }
  };

  tm.addTask(&task_charger);

Obviously i will refine this a lot but it seems to work really well for what it is right now. Screenshot 2023-11-02 115347

Now my question, are you interested in PRs like this?

BernardoGiordano commented 1 year ago

Yeah, PRs like this would be really appreciated.

Slluxx commented 1 year ago

Awesome, i will continue with this and make a PR once everything is ironed out.

Are you able to update mongoose even further by any chance? Possibly to the latest version? It improved a lot, resulting in way less self-written network code and much higher readability & maintainability.

Also, what are your thoughts on simply creating a wakelock to get around the thread stalling after standby?

BernardoGiordano commented 1 year ago

I was trying to migrate to the latest mongoose release at some point but I stopped because it wasn't working properly and I needed to get a working PoC as fast as possible. I guess updating the library would be useful. I'm not currently able right now, probably from next week.

Also, what are your thoughts on simply creating a wakelock to get around the thread stalling after standby?

It can be tried, sure.