libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [libmicrohttpd] websocket broadcast to all clients


From: Christian Grothoff
Subject: Re: [libmicrohttpd] websocket broadcast to all clients
Date: Wed, 20 Jul 2022 06:26:25 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0

Dear Bill,

You should use a "MHD_NotifyConnectionCallback" to be notified about
when MHD is 'finished' with a connection.  Then, you can insert the
connection when the websocket mode is started into a data structure
(usually a doubly-linked-list (DLL) is better than an array) and remove
it during the above callback. To 'broadcast', you'd then iterate over
the DLL to send on each connection. Pseudocode:

struct ConnectionDllEntry {
  struct ConnectionDllEntry *next;
  struct ConnectionDllEntry *prev;
  struct MHD_Connection *conn;
  // other data structures you keep per connection
};

Then make sure to keep the above in the 'socket_context' you have per
MHD connection, and to clean up when the connection is finished:

void
my_notify_connection_cb (void *cls,
                         struct MHD_Connection *connection,
                         void **socket_context,
                         enum MHD_ConnectionNotificationCode toe)
{
  struct ConnectionDllEntry *cde = *socket_context;

  assert (connection == cde->connection);
  REMOVE_FROM_DLL (cde_head, cde_tail, cde);
  free (cde);
}

Anyway, I hope those bits give you enough of the idea.

Happy hacking!

Christian

On 7/19/22 23:47, Bill Bowling wrote:
> I've been using libmicrohttpd for many years and have recently become
> interested in using websockets.
> I have a working version that seems to do what I want with a single client.
> But, I want to broadcast a response to all connected clients.
> 
> I've searched through many posts and documents but have not stumbled
> across how this could be done.
> 
> I think if I could place the connections in an array I could then loop
> through them.
> I have not been able to find enough information to pull this off.
> 
> Any help would be appreciated.
> 
> -- 
> Thanks,
> Bill Bowling



reply via email to

[Prev in Thread] Current Thread [Next in Thread]