Theldus / wsServer

wsServer - a tiny WebSocket server library written in C
https://theldus.github.io/wsServer
GNU General Public License v3.0
422 stars 80 forks source link

I had a blast with your wsServer library the last few days. #24

Closed dinther closed 2 years ago

dinther commented 3 years ago

I spend my entire career trying to avoid C so despite decades in IT I only spend a week so far on learning C.

For a project I need to send messages from a browser to a raspberry pi zero w. In turn it drives TMC2209 stepper controllers etc etc. Your library is indeed a breeze to start with and I have it working nicely receiving binary data.

Now I wonder, is it possible to have an animation loop running while wsServer is doing it's thing?

ws_socket(&evs, 8080);

Hogs the main loop and I can't figure out how I can keep my own code going while we wait for the next message. Maybe a hook for a callback or a function call so I can push the wsServer loop along manually. Maybe there is a solution already but after seeing the thread stuff I decided to grease up and see if you have any ideas.

Cheers.

Paul.

Theldus commented 3 years ago

Hi @dinther, (I'm glad you liked it, it's feedback like this that pushes me forward for keeping this library, thanks =).)

About your issue, yes, you're right. So far, there wasn't any kind of feature that would make the ws_socket non-blocking. With that in mind, the latest commit slightly modifies the ws_socket function signature to:

int ws_socket(struct ws_events *evs, uint16_t port, int thread_loop);

When the thread_loop parameter is zero, the routine behaves as before; when non-zero, a thread is created to accept new connections, and the function returns right away.

So from now on, a code like the one below would work without problems:

int main(void)
{
    int i;
    struct ws_events evs;
    evs.onopen    = &onopen;
    evs.onclose   = &onclose;
    evs.onmessage = &onmessage;
    ws_socket(&evs, 8080, 1);

    i = 0;
    while (1) {
        sleep(1);
        printf("%d seconds have passed...\n", ++i);
    }

    return (0);
}

Maybe a hook for a callback or a function call so I can push the wsServer loop along manually.

That would be very interesting. I've seen some libraries using a similar approach, letting the user have full control over connections, receiving messages, and so on. Maybe it's a good feature to support in the future.


Anyway, please let me know what you think of my changes and if they meet what you expect.

dinther commented 3 years ago

Oh man, you're a saint. It's late here in New Zealand but I definitely try the update this weekend and see if I can animate my variables.

I am sending command speeds for multiple stepper motors via your websocket and then onto a TMC2209 which generates it's own step pulses at a commanded rate. But stepper motors need a managed acceleration ramp otherwise they stall. With the loop suggested I can make sure the stepper motor is guided towards the target speed.

Thank you so much.

dinther commented 3 years ago

I couldn't wait of course :-)

Actually, I could not make it work initially and wrote you an elaborate message with code details but every time I thought...hang on let's check this or that as I didn't want to look like an idiot.

Well, all issues were my own fault of course but I got it working thanks to your change. Thank you very much. I let you know if I find any issues during testing.

Theldus commented 3 years ago

I'm glad you made progress with the library and your use case (which looks very interesting). Any questions, don't hesitate to ask =).

Theldus commented 2 years ago

I am going to close this issue. Feel free to create other issues whenever you need =).

PS: I created an issue (#47) covering what we talked about a lower-level API, as it's a feature I really want and I couldn't forget to implement.