[Libwebsockets] Creating a WebSocket from a regular socket instance

Andy Green andy at warmcat.com
Wed Mar 11 16:29:22 CET 2015

On 11 March 2015 23:09:45 GMT+08:00, Danilo Guanabara Fernandes <danguafer at gmail.com> wrote:
>Hello everyone. It's my first post here.
>I am coding a protocol independent server and I would like to know if
>is any way to create a WebSocket client from an 'accept'ed socket which
>am going to peek for any handshake message to define the underlying

If I understand it, somebody else accepted the connection and wants to hand it off together with a buffer of already-received data from the connection.  Eg, existing webserver that doesn't know how to deal with ws itself.

There's no support for it.  But I should think it can work if you want to hack it in as a cmake option (it should not bloat builds that don't need it).

Basically you need something like

 - define a new connect api which accepts a struct with the fd, pointer to the received data and its length

 - get the new connect api to do the same as the normal one, but set a flag on the wsi to say it has the goodies struct attached

 - set the fd directly don't make a new socket and set the connection state to the end of the client connect sequence

 - there's already code related to SSL that forces poll() to "fire" if the SSL connection has buffered content.  Re-use that to deliver the buffered recieved data as if it just came from the socket; when the buffer is exhausted switch to normal socket poll semantics (again see how the SSL buffered data thing works)


>Thanks in advance,
>Libwebsockets mailing list
>Libwebsockets at ml.libwebsockets.org

More information about the Libwebsockets mailing list