[Libwebsockets] Multiple Connections on vhosts
andy at warmcat.com
Thu Aug 30 22:30:36 CEST 2018
On August 31, 2018 1:13:15 AM GMT+08:00, Derek Bailey <dbaileychess at gmail.com> wrote:
>I understand there is one 'context' that can have multiple 'vhosts'
>the LWS_SERVER_OPTION_EXPLICIT_VHOSTS option is set), which can have
>multiple protocols. And these 'vhosts' are created using the
>'lws_create_vhost' function in the same thread running the
>I am curious on how to properly perform a new client connection
>('lws_client_connect_via_info') in one of these vhosts, from an
>thread. In the minified client example:
Lws is singlethreaded, only a couple of apis are able to be called from foreign threads. So you just can't randomly start calling lws apis from multiple threads. You need to take the approach to signal the service thread to do it.
>The 'default' vhost is created automatically and the
>'LWS_CALLBACK_PROTOCOL_INIT' callback is handled and that is where the
>connection first occurs. When I tried to adapt the code to allow
>distinct connections to be initiated externally, I naively create new
>'vhosts' for each connection, and used the same calllback to init. But
It's unlikely that makes any sense. Vhosts for client connections are basically a) a place to hang the client lws_protocol and callback, and b) a place to hang the related tls context. Because the per-wsi callbacks for client are separate than those for server, you can usually use an existing server-side lws protocol to handle client connection callback stuff too, so a) isn't a problem. If no special tls requirement per-connection then re-using an existing vhost client tls ctx using the system CA trust store is also OK. In other words, typically no problem using lws_protocols you already have on an existing vhost, even the default one if you don't otherwise need separate vhosts.
>vhost can support multiple client connections (with different
Of course, there is no restriction just as there's no limit other than ulimit fd limit for serving multiple clients in a vhost.
>what is the best way to do it from an external thread? Do i have
>this step in the main service thread and call 'lws_cancel_service' to
Yeah. Cancel service (at least on *nix) just writes a byte into a pipe that lws inserted into the poll wait list for this purpose. That makes it cleanly threadsafe. In the callback, you can check a shared struct protected by a mutex if needed to see if other threads wanted you to do something.
>I think the examples are great! It would be nice to have one that shows
>multiple clients can be created in the same process, using the same
>context. All the examples that I saw for clients assume a single
They certainly cut down on the number of times the same basic questions were coming and I have not heard, "how can I get started" since the kitchen-sink test app stopped being the only real example. And I stopped seeing people bringing the same broken third party 'tutorial' code from years ago from the Internet asking why it doesn't work.
shows the 'best practice' for rate-limited client connection retries for ws. If you're doing http client rather than ws, also note lws does transparent connection optimization of multiple concurrent client requests to the same host + port, the http-client-multi example will be interesting then.
More information about the Libwebsockets