[Libwebsockets] Detecting websocket connections

Andy Green andy at warmcat.com
Fri Nov 18 22:17:15 CET 2016

On November 19, 2016 3:10:48 AM GMT+08:00, Alan Conway <aconway at redhat.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_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)


>Libwebsockets mailing list
>Libwebsockets at ml.libwebsockets.org

More information about the Libwebsockets mailing list