[Libwebsockets] Send data with libwebsockets

Stefano Sabatini stefasab at gmail.com
Wed Oct 29 21:37:10 CET 2014


In data Wednesday 2014-10-29 07:53:11 +0800, Andy Green ha scritto:
[...]
> >My understanding is that I have to set NO_FIN on the last packet. But
> >then I get this:
> 
> You want a FIN *only* on the last fragment, it's the opposite of your understanding.  Your code above has the right idea but it does not handle the last read from the file correctly.
> 
> FIN bit indicates this fragment completes a ws message.  So you want to withold FIN (NO_FIN) on all the fragments except the last one in your message.

Now I have this:

    case LWS_CALLBACK_SERVER_WRITEABLE:
    {
        /* send data segment */
        int n, flags = LWS_WRITE_BINARY | LWS_WRITE_CONTINUATION;
        int is_end = 0;

        n = fread(buf + LWS_SEND_BUFFER_PRE_PADDING, 1, PAYLOAD_SIZE, user_data->file);
        if (feof(user_data->file)) {
            fclose(user_data->file);
            user_data->file = NULL;
            is_end = 1;
        } else {
            flags |= LWS_WRITE_NO_FIN;
        }

        printf("Sending %d bytes%s...\n", n, is_end ? "[END]" : "");
        libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], n, flags);
        if (!is_end)
            libwebsocket_callback_on_writable(context, wsi);
    }
    break;

My understanding is that I have to enable the continuation opcode on
all message segments, and disable the FIN bit (setting NO_FIN) on all
but the last segment.

Still this is not working, I got this on the log:
New Connection established with client
Received data: Hello WebSocket!
Sending image data from file lena.jpeg
Sending 4096 bytes...
lwsts[27634]: lws_write: unknown write opc / protocol
Sending 4096 bytes...
lwsts[27634]: lws_write: unknown write opc / protocol
Sending 4096 bytes...
lwsts[27634]: lws_write: unknown write opc / protocol
Sending 4096 bytes...
lwsts[27634]: lws_write: unknown write opc / protocol
Sending 17 bytes[END]...

The client onMessage callback hangs, as it was not receiving the
message.

If I do instead:
    case LWS_CALLBACK_SERVER_WRITEABLE:
    {
        /* send data segment */
        int n, flags = LWS_WRITE_BINARY;
        int is_end = 0;

        n = fread(buf + LWS_SEND_BUFFER_PRE_PADDING, 1, PAYLOAD_SIZE, user_data->file);
        if (feof(user_data->file)) {
            fclose(user_data->file);
            user_data->file = NULL;
            is_end = 1;
        } else {
            flags |= LWS_WRITE_NO_FIN | LWS_WRITE_CONTINUATION;
        }

        printf("Sending %d bytes%s...\n", n, is_end ? "[END]" : "");
        libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], n, flags);
        if (!is_end)
            libwebsocket_callback_on_writable(context, wsi);
    }
    break;

The client seems to receive the message, but only the last segment. In
attachment the complete code.

Removing the LWS_WRITE_CONTINUATION causes the connection to be closed
on the last segment sent:
lwsts[29345]: error on reading from skt
Connection closed

What am I still doing wrong?

> 
> >New Connection established with client
> >Received data: Hello WebSocket!
> >Sending image data from file lena.jpeg
> >Sending 4096 bytes...
> >Sending 4096 bytes...
> >Sending 4096 bytes...
> >Sending 4096 bytes...
> >Sending 17 bytes...
> >lwsts[21381]: error on reading from skt
> >
> >This is not still working (usually it bails out after a few packets),
> >or before sending the final 0-sized packet.
> >

> >Indeed I'm not sure about how should I use NO_FIN and CONTINUATION (I
> >tried with several combinations of them, none worked so far), and I
> >can see no documentation about them in libwebsockets.h
> 
> test-fraggle shows how to use them.

Yet clarifying the flags use in the doxy would be immensely useful,
I'm probably alone but I can't figure out the meaning of them by
reading it.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: send-image-server.c
Type: text/x-csrc
Size: 3916 bytes
Desc: not available
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20141029/090ab25c/attachment.bin>


More information about the Libwebsockets mailing list