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

re-send to all clients ? #36

Closed hannesa2 closed 2 years ago

hannesa2 commented 2 years ago

Hi, I play around with your lib and see, that I can resend the received message to sender only

void onmessage(int fd, const unsigned char *msg, uint64_t size, int type) {
    char *cli;
    cli = ws_getaddress(fd);
    printf("I receive a message: %s (size: %" PRId64 ", type: %d), from: %s/%d\n", msg, size, type, cli, fd);
    free(cli);

    ws_sendframe(fd, (char *)msg, size, true, type);
}

How to send a message to all (or special) connected clients ? Maybe my question is to general, but I'm a C rooky and maybe there is already such a list of clients ?

Theldus commented 2 years ago

Hi @hannesa2, The signature of the ws_sendframe* functions follows below:

int ws_sendframe_txt(int fd, const char *msg, bool broadcast);
int ws_sendframe_bin(int fd, const char *msg, uint64_t size, bool broadcast);
int ws_sendframe(int fd, const char *msg, uint64_t size, bool broadcast, int type);

where the bool (broadcast) type parameter sends the message to all connected clients when true, and only to fd if false. I believe your example above does exactly what you want.

However, if you only want to send to a few clients, this has already been discussed here, but in summary, you need to handle this manually (keeping a list of all connected clients), as so far, wsServer do not exports the list of connected clients.

It's a feature I intend to work on.