[Libwebsockets] question about multi-protocols in libwebsockets

Andy Green andy at warmcat.com
Thu Jul 16 07:57:49 CEST 2020



On 7/16/20 6:24 AM, Felix Zhang wrote:
> Hi Andy,
> 
> Hope you are doing well.

I wish...

> I have one question about how to support multiple subprotocol in on a 
> websocket connection.
> for example,
> client sends "Sec-WebSocket-Protocol: protocol-a, protocol-b, protocol-c"
> and server selects only "protocol-a" and "protocol-c" and the response 
> like" Sec-WebSocket-Protocol: protocol-a, protocol-c ".

That's not quite how it works, the server must pick one that it likes 
best and let the client know what it picked.  Since it will be running 
only one negotiated protocol over an individual socket.

The client then typically binds to the local lws_protocols struct with 
the selected name, allocates user data according to that etc.

> Is this a common case? and do we support this case today?
> Thanks in advance.

Client offering multiple protocols is a normal, standardized case.

The lws client info struct member can take a comma-separated list:

https://libwebsockets.org/git/libwebsockets/tree/include/libwebsockets/lws-client.h#n84-85

On the server side, it goes through what the client sent from left to 
right, and picks the first one it has support for:

https://libwebsockets.org/git/libwebsockets/tree/lib/roles/ws/server-ws.c#n496-536

It's not specified in RFC6455 but it's assumed the client placed them in 
order of highest preference first.

-Andy


More information about the Libwebsockets mailing list