Theldus / wsServer

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

Clients do not receive send frames #62

Closed JonasB2497 closed 2 years ago

JonasB2497 commented 2 years ago

Hi, I use this great library in a small project of mine where I want to send a status message when a new client connects. I first tried using ws_sendframe_txt() in the onopen() function to broadcast to all clients (using NULL as the client parameter) which did not work. No error was thrown as far as I could see but the clients didn't receive the send frame. Using the provided client parameter however worked as expected.

Theldus commented 2 years ago

Hi @JonasB2497, Thanks for giving wsServer a try,

Interesting, I just tested the code below (modifying the examples/echo/echo.c example) and it worked as expected: all clients (except itself) received the broadcast message:

#include <stdio.h>
#include <ws.h>

void onopen(ws_cli_conn_t *client){
    ws_sendframe_txt(NULL, "new client connected!");
}

void onclose(ws_cli_conn_t *client){}
void onmessage(ws_cli_conn_t *client,
  const unsigned char *msg, uint64_t size, int type){}

int main(void)
{
    struct ws_events evs;
    evs.onopen    = &onopen;
    evs.onclose   = &onclose;
    evs.onmessage = &onmessage;
    ws_socket(&evs, 8080, 0, 1000);
    return (0);
}

How are you testing? (your environment settings, code used, websocket client, and etc). Could you tell me if the code above works as expected for you?


Edit: Now I see what you meant (I think): the broadcast message is always sent to all clients (including itself), but it doesn't happen in the on_open() event. Since this deviates from the expected behavior (always send broadcast to all active clients), I consider this a bug and commit 7887520840ec999c3fc5e50bb35235f6259eadd3 fixes it.

Please let me know if this works for you now...

JonasB2497 commented 2 years ago

Edit: Now I see what you meant (I think): the broadcast message is always sent to all clients (including itself), but it doesn't happen in the on_open() event. Since this deviates from the expected behavior (always send broadcast to all active clients), I consider this a bug and commit https://github.com/Theldus/wsServer/commit/7887520840ec999c3fc5e50bb35235f6259eadd3 fixes it.

Yes, this is exactly what I meant!

I am running the code on a Raspberry Pi 4B and the client is a normal browser (Firefox and Chrome). Your code is basically very similar to mine. (I just call another function in onopen() which in turn puts together some messages and calls ws_sendframe_txt() function multiple times but this should makes no difference.) It reproduced the behavior I experienced it and with the new commit it now behaves as I would have expected it.

Thank you for the quick fix!