Theldus / wsServer

wsServer - a tiny WebSocket server library written in C

Home Page:https://theldus.github.io/wsServer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Clients do not receive send frames

JonasB2497 opened this issue · comments

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.

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 7887520 fixes it.

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

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 7887520 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!