[Libwebsockets] How to add virtual host dynamic

Andy Green andy at warmcat.com
Fri May 4 12:01:44 CEST 2018



On 05/04/2018 05:16 PM, Chropin Hu wrote:
> Dear All,
>        I am looking for the way to add virtual host dynamically.
>        I have referenced to the implement of lwsws.
>        For the lwsws  case, the step looks like:
>             1,    create lws_context
> 2, create event loop (for the libuv case)
> 3, create the vhost from config
> 4, start event loop.
> 
> So, For the dynamic add virtual host case, I am trying to implement it as:
> 1, start the host1 as lwsws does;
> 2, call las_create_vhost to create a new vhost2 at http response callback.

A clearer example to follow now is

https://github.com/warmcat/libwebsockets/tree/master/minimal-examples/http-server/minimal-http-server-multivhost

You can dynamically add a vhost later the same way, it doesn't have to 
be done first.  It's also possible to destroy the vhost

/**
  * lws_vhost_destroy() - Destroy a vhost (virtual server context)
  *
  * \param vh:	pointer to result of lws_create_vhost()
  *
  * This function destroys a vhost.  Normally, if you just want to exit,
  * then lws_destroy_context() will take care of everything.  If you want
  * to destroy an individual vhost and all connections and allocations, you
  * can do it with this.
  *
  * If the vhost has a listen sockets shared by other vhosts, it will be 
given
  * to one of the vhosts sharing it rather than closed.
  */
LWS_VISIBLE LWS_EXTERN void
lws_vhost_destroy(struct lws_vhost *vh);

The vhost destroy stuff is staged in two parts, one initates closure of 
all wsi on the vhost etc, and later, asynchronously, it's actually 
destroyed.  So it should be OK to destroy a vhost from a callback of a 
wsi bound to the vhost.

-Andy

> I am not sure if this is the right thing to do.
> 
> Thanks for any suggestions …
> 
> Yours Chropin.
> 
> 
> 
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> https://libwebsockets.org/mailman/listinfo/libwebsockets
> 



More information about the Libwebsockets mailing list