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

How can I create a list of connected clients using the wsServer library? (question)

Sarang0218 opened this issue · comments

Greetings,

I am currently working with the wsServer library and I am trying to achieve two tasks. First, I need to create a list of connected clients. Second, I want to broadcast a message to some of the clients on the list based on their file descriptor (FD).

I attempted to create an array of connected clients using the syntax ws_cli_cont clients[size]. However, I received an error message indicating that ws_cli_cont is not a complete type. I am not sure if this is the correct way to create an array of connected clients.

Additionally, I need to differentiate between clients based on their FD to broadcast a message only to a specific group of clients. Can someone guide me on the proper way to create a list of connected clients and broadcast a message based on FD using the wsServer library? Any advice or code examples would be greatly appreciated.

Thank you in advance.

I think you should use pointer type like ws_cli_cont *clients[size].
In the onopen callback, you can add the pointer to the array.

In the onclose callback, you should remove the pointer from the array.

I havn't tested this code. But I think this code could help you

ws_cli_conn_t *clients[64];

void onopen(ws_cli_conn_t *client)
{
    char *cli;
    cli = ws_getaddress(client);
    printf("Connection opened, addr: %s\n", cli);
    for(int x = 0; x < 64; x++){
        if(NULL == clients[x]){ 
            clients[x] = client;    // Add the client pointer to the array
            break;
        }
    }
}

void onclose(ws_cli_conn_t *client)
{
    char *cli;
    cli = ws_getaddress(client);
    printf("Connection closed, addr: %s\n", cli);
    for(int x = 0; x < 64; x++){
        if(client == clients[x]){   //compare the pointer value
            clients[x] = NULL;    // remove the client pointer from the array
            break;
        }
    }
}

Thanks, I’ll check this out as soon as I return home.

Works Great!