[Libwebsockets] Multiple ports in one thread

Andy Green andy at warmcat.com
Mon Apr 11 13:46:54 CEST 2016

On 04/11/2016 07:30 PM, Ondra Čopák wrote:
> Hello,
> I have a question regarding this simple case:
> I have two separate contexts (contextA and contextB). Each has a bit
> different settings (SSL, port).
> I want to service both context on a single thread.
> So far i use:
> lws_service(contextA, 0);
> lws_service(contextB, TIMEOUT);
> which is not completely correct (can miss something on contextA while
> waiting on contextB).
> What is the correct approach to solve this?
> Would it be worthwhile to have a lws_service(array_of_contexts,
> array_size, timeout) which takes several LWS contexts and services all
> of them?

Ahaha... this is a good time for that question ^^

If you use master branch, since 1.7 there is a new concept of "vhost" 

By default, when you create the context, a single vhost is implied and 
everything is as before.

However if when you create the context you give the options flag 
"LWS_SERVER_OPTION_EXPLICIT_VHOSTS", no vhost or listen socket is 
created, only the logical context.

There is a new api lws_create_vhost() which you may call as many times 
as you want logical vhosts, it's a linked list so there is no fixed 
limit.  The argument is basically the same info struct used to create 
the context, but you adapt the members related to the vhost each time.

Where the listen socket is already created by an earlier vhost (eg, 
multiple vhosts on 443) then SNI is automatically used against the vhost 
name to bind connections to the right guy.

Where the port differs, multiple listen sockets are spawned as needed 
inside the same context (and single service action).

To leverage these new library features, and serve as example code, there 
is a new 'lwsws' webserver app included in master.


This allows you to define the vhosts in JSON config files, and implement 
ws protocols as plugins.  Basically it gives you the choice to get away 
from cut-and-paste of the test server and implement inside a generic 
server, with just your ws protocol standalone as a plugin, eg,



> Thanks a lot!
> Best regards,
> Ondrej
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> http://libwebsockets.org/mailman/listinfo/libwebsockets

More information about the Libwebsockets mailing list