Device Screen

Radikant WebSocket c 

Radikant WebSocket is a simple, cross-platform C “shim" that provides a simple and consistent API for WebSockets.

More Info

Introduction

Radikant WebSocket is a simple, cross-platform C library that provides a simple API driving WebSockets. It offers both synchronous and asynchronous interfaces. It uses Radikant Socket, and  Radikant TLS to establish a connection in either server of client mode.

Currently only the client is usefull, because Radikant TLS, Radikant Socket and Radikant Event must be refactored to a more sane and scalable architecture to provide better decoupling for async server capabilities and mulitple client handling. 

These libraries currently follow anti-patterns. Most likely some kind of top level orchestration layer needs to be created (possibly Radikant-Connect-C that uses these low-level drivers as pure state machines but orchestrates with clear responsibilities and seperation of concerns. 

For example i’m researching how to handle mulitple WebSockets clients in a server context, Where should this be managed? In TLS, socket or WebSocket? Should we use use Radikant Event or create a smaller event only lib.

FileEditViewProject
WebSocketTest — Echo Client
int main(int argc, char* argv[]) {
const char* url = "wss://localhost:8082";
rws_client_t* ws = rws_connect(url);
 
const char* messages[] = {
"hello_message_1",
"hello_message_2",
"hello_message_3",
"hello_message_4",
"hello_message_5"
};
int msg_count = 5;
 
for (int m = 0; m < msg_count; m++) {
const char* msg = messages[m];
if (rws_send_text(ws, msg) != RWS_OK) {
fprintf(stderr, "[ERROR] Send failed\n");
rws_close(ws);
return 1;
}
 
printf("[TEST] Receiving...\n");
void* data = NULL;
size_t len = 0;
rws_opcode_t op;
// Loop to handle potential welcome messages or previous chatter
int max_attempts = 5;
int echo_found = 0;
 
for (int i = 0; i < max_attempts; i++) {
rws_error_t err = rws_recv(ws, &data, &len, &op, 5000); // 5s timeout
if (op == RWS_OP_TEXT) {
printf("[TEST] Payload: %s\n", (char*)data);
if (strcmp((char*)data, msg) == 0) {
printf("[PASS] Echo matches!\n");
echo_found = 1;
if (data) free(data);
break;
}
}
if (data) free(data);
}
}
 
rws_close(ws);
}