[Libwebsockets] Using protocols not passed to lws_create_context()

Andy Green andy at warmcat.com
Tue Jan 14 12:37:06 CET 2020



On 1/14/20 11:06 AM, Zevv wrote:
> Hi list,
> 
> I'm in the process of migrating an propriatary application with a custom
> http socket layer to libws. There are a number of different websocket
> client modules in different parts of the code, and I'd like to keep
> those logically separated.
> 
> My problem is that all the lws_protocols are defined at the lws_context
> level, so I'm not able to define a protocol in a local source file only,
> as they all need to be registered in one central place to the
> lws_context at lws_create_context() time.
> 
> My current workaround is to create multiple lws_contexts in separate
> modules, but this is of course not the right way to go.

Yes that's really not the right way to solve it.  You want a single 
lws_context / event loop.

There are a few different things that might help.

> Is it possible to bind a protocol and/or wsi callback on a wsi without
> having to define these when creating the lws_context?

Sort of... first, protocol init happens at vhost creation time, not 
context creation time, the protocols bind to a vhost not the whole 
context.  By default, for backwards compatibility, lws creates you a 
default vhost at context creation time with whatever list of protocols 
you gave then, and the two things are coming in one step.

But that's not required, you can tell lws at context creation time 
you're going to explicitly define vhosts yourself

https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/http-server/minimal-http-server-multivhost/minimal-http-server.c#n110

with that you can individually control which protocols are bound to 
which vhost.  It's possible to create vhosts dynamically once the 
context is there.  Notice you need to provide a PVO (per-vhost option) 
naming each protocol you want to enable on each vhost when you define 
the vhosts explicitly.

https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/ws-server/minimal-ws-server-threads/minimal-ws-server.c#n73
https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/ws-server/minimal-ws-server-threads/minimal-ws-server.c#n110

(A side note something that was added before v3.2 that really helps with 
protocol encapsulation is you can now set the protocols on the vhost 
using .pprotocols, which takes an array of pointers to the lws_protocols 
structs, rather than an array of the structs.

https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/http-server/minimal-http-server-dynamic/minimal-http-server-dynamic.c#n184
https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/http-server/minimal-http-server-dynamic/minimal-http-server-dynamic.c#n274

That means you just need to export the pointer from your protocol 
implementation)


Second lws has a plugins concept where protocols, eg

https://libwebsockets.org/git/libwebsockets/tree/plugins/protocol_lws_mirror.c

which can be completely built standalone as well

https://libwebsockets.org/git/libwebsockets/tree/plugin-standalone

are built into dynamic lib plugins... these are discovered at startup if 
LWS_WITH_PLUGINS enabled.  You can then use per-vhost options to enable 
specific protocol names on specific vhosts.


Third, this is made a lot more easy-to-manage if you use either lwsws or 
at least lejp-conf.c to control which protocols are on which vhosts on 
which ports / interfaces in JSON, eg

https://libwebsockets.org/git/libwebsockets/tree/lwsws/etc-lwsws-conf.d-localhost-EXAMPLE

in fact the whole of warmcat.com / libwebsockets.org is served by lwsws 
using protocol plugins + JSON config to control what appears where, 
without having any special application, even though it serves complex 
things like https://libwebsockets.org/testserver, 
https://libwebsockets.org/git (this is a reverse proxy for a gitohashi 
instance on the same machine that is serving over Unix Domain Sockets), 
https://libwebsockets.org/mailman (this is a CGI) and from the same 
server on a different vhost, https://warmcat.com, 
https://warmcat.com/git etc, all from JSON.

-Andy

> Thank you,
> 
> Zevv
> 
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> https://libwebsockets.org/mailman/listinfo/libwebsockets
> 


More information about the Libwebsockets mailing list