[Libwebsockets] "Split" HTTP Responses

Andreas Wetzel andreas.wetzel at nanotronic.ch
Tue Feb 26 11:38:24 CET 2013


I just wanted to let you know about the "hacky" solution I found for my 
problem (now using libwebsockets v1.22):

In the LWS_CALLBACK_HTTP i send the appropriate HTTP header using 
libwebsocket_write (which results in a basic "write" syscall). I then 
acquire the file-descriptor for the connection using 
libwebsocket_get_socket_fd, before I return 0 from the callback.
Returning zero will prevent libwebsockets from closing the socket when 
the callback is left.

Now, whenever I have new data to send to the client, i write it to the 
acquired file descriptor - completely separate from libwebsockets.
As soon as I've transferred all my data, I call close() on the 
file-descriptor, which lets libwebsockets do its final cleanup.

This for shure is neither a clean nor good way, but a working solution 
for my exact use-case.


On 02/14/2013 03:29 PM, Drew Noakes wrote:
> I too would be interested in an example that shows how to formulate a
> non-file response to a web request through libwebsockets. My use case is
> similar -- I'd like a client to be able to request a static dump of some
> data, probably as a JSON document. This would be composed dynamically in
> the same process as my libwebsockets server.
> Drew.
> On 14 February 2013 13:42, "Andy Green (林安廸)" <andy at warmcat.com
> <mailto:andy at warmcat.com>> wrote:
>     On 14/02/13 21:34, the mail apparently from Andreas Wetzel included:
>         Hi,
>         Currently, I'm using libwebsockets v1.1 to stream live
>         sensor-data to a
>         web-interface. the data is accumulated outside of libwebsockets
>         thread,
>         submitted to client on "writeable" callback. Everything works as
>         I would
>         expect it.
>         Now, I want to provide a method to save the sensor data-stream
>         to a file
>         (on the client-side). Easiest way to do so seems to be without
>         websockets, but using a plain HTTP download.
>         I tried to achieve a similar functionality using the
>         callback. But there seems no counterpart to the
>         LWS_CALLBACK_SERVER_WRITEABLE callback for HTTP (or non-websockets)
>         connections. Is that true?
>         Does anybody know how to handle that? Or is there another (better?)
>         method to send live-data to a file on client-side?
>         Any help would be highly appreciated.
>     You are right, it's missing right now.
>     Originally the HTTP stuff was just added to allow lws to deliver a
>     "seed" html script that would make the websocket connections from
>     the client back to the server, as you can see in the test server.
>       That works fine.
>     The http file transfer stuff has been upgraded recently to use
>     "writable" callbacks, and to stuff the send channel until just
>     before it would block, but it's special for the case you have a
>     physical file on the server you want to transfer: there's no general
>     way to take care of writeability callback in http as there is in
>     websocket protocol.
>     Actually I think that is pretty desirable to also provide it for
>     http. I'll take a look at adding it shortly / tomorrow.
>     -Andy
>     _________________________________________________
>     Libwebsockets mailing list
>     Libwebsockets at ml.__libwebsockets.org
>     <mailto:Libwebsockets at ml.libwebsockets.org>
>     http://ml.libwebsockets.org/__mailman/listinfo/libwebsockets
>     <http://ml.libwebsockets.org/mailman/listinfo/libwebsockets>

Andreas Wetzel
nanoTRONIC GmbH // Hintermättlistrasse 9 // CH-5506 Mägenwil

More information about the Libwebsockets mailing list