[Libwebsockets] Detecting websocket connections

Alan Conway aconway at redhat.com
Fri Nov 18 22:52:54 CET 2016


On Sat, 2016-11-19 at 05:17 +0800, Andy Green wrote:
> 
> On November 19, 2016 3:10:48 AM GMT+08:00, Alan Conway <aconway at redha
> t.com> wrote:
> > 
> > I'm listening for connections outside of libws, then I want libws
> > to
> > adopt the sockets. Is it possible to examine protocol headers and
> > decide whether the connection is websocket-wrapped or not so I can
> > accept WS and non-WS connections on the same port?
> 
> You can
> 
> /**
>  * lws_adopt_socket_readbuf() - adopt foreign socket and first rx as
> if listen socket accepted it
>  * \param context:	lws context
>  * \param accept_fd:	fd of already-accepted socket to adopt
>  * \param readbuf:	NULL or pointer to data that must be
> drained before reading from
>  *		accept_fd
>  * \param len:	The length of the data held at \param readbuf
>  *
>  * Either returns new wsi bound to accept_fd, or closes accept_fd and
>  * returns NULL, having cleaned up any new wsi pieces.
>  *
>  * LWS adopts the socket in http serving mode, it's ready to accept
> an upgrade
>  * to ws or just serve http.
>  *
>  * If your external code did not already read from the socket, you
> can use
>  * lws_adopt_socket() instead.
>  *
>  * This api is guaranteed to use the data at \param readbuf first,
> before reading from
>  * the socket.
>  *
>  * readbuf is limited to the size of the ah rx buf, currently 2048
> bytes.
>  */
> LWS_VISIBLE LWS_EXTERN struct lws *
> lws_adopt_socket_readbuf(struct lws_context *context, lws_sockfd_type
> accept_fd,
> const char *readbuf, size_t len);
> 
> (But of course for the record lws is also perfectly capable of
> 'serving http[s] and ws[s] on the same port' all by itself)
> 

Beautiful :) The http[s] part is going to be useful too. Basically we
have an AMQP router with a javascript management console. So it looks
like on a single port I can accept AMQP management direct, AMQP wrapped
in websockets from a browser console AND I can serve up the console
javascript direct from the router. Exactly what I want :)





More information about the Libwebsockets mailing list