Skip to content

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

Closed

Description

@Sarang0218

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.

Activity

  1. raspberry-pi-maker commented on Feb 28, 2023

    @raspberry-pi-maker

    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;
            }
        }
    }
  2. Sarang0218 commented on Feb 28, 2023

    @Sarang0218
    Author

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

  3. Sarang0218 commented on Feb 28, 2023

    @Sarang0218
    Author

    Works Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions