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

API Changes #45

Closed Theldus closed 2 years ago

Theldus commented 2 years ago

Description

A few days ago I was asked why wsServer only sends broadcast messages if it knows a client beforehand (its 'fd'). Clearly this is an issue as the broadcast must work without the prior knowledge of any connected users.

This PR therefore fixes this bug by changing the signature of the ws_sendframe* routines: if a client is present, send only to it, otherwise broadcast.

In addition, the 'fd' parameter present in the API functions was replaced with 'ws_cli_conn_t', which made the code more intuitive and also allowed a great internal cleaning of the source code.

The new function signatures:

int ws_sendframe(ws_cli_conn_t *cli, const char *msg, uint64_t size, int type);
int ws_sendframe_txt(ws_cli_conn_t *cli, const char *msg);
int ws_sendframe_bin(ws_cli_conn_t *cli, const char *msg, uint64_t size);
int ws_get_state(ws_cli_conn_t *cli);
int ws_close_client(ws_cli_conn_t *cli);

and the events:

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

A huge thanks to @tinkering4fun and @troglobytor for the discussion over the last few days on how I should approach this issue. I'm happy with the result and hope it meets your expectations.

Feel free whenever you want to suggest something... open an issue, discussion, PR... I'm always trying to listen to the community and adapt wsServer to their needs.