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.



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);
}
    
Radikant
File
Edit
View
Window
Help
WebSocket TLS.log
[TEST] Client Connected (Secure)! [TEST] --- Round 1: Sending 'hello_message_1' --- 19:46:57 [INFO] Encrypting 6 bytes for record type 23 19:46:57 [INFO] Encrypting 15 bytes for record type 23 [TEST] Receiving... [TEST] Recv Success. Op: 0x1, Len: 32 [TEST] Payload: Request served by 4d896d95b55478 [INFO] Message mismatch (likely welcome msg). Listening again... [TEST] Recv Success. Op: 0x1, Len: 15 [TEST] Payload: hello_message_1 [PASS] Echo matches! [TEST] --- Round 2: Sending 'hello_message_2' --- 19:46:57 [INFO] Encrypting 6 bytes for record type 23 19:46:57 [INFO] Encrypting 15 bytes for record type 23 [TEST] Receiving... [TEST] Recv Success. Op: 0x1, Len: 15 [TEST] Payload: hello_message_2 [PASS] Echo matches! Record 3-5