[Libwebsockets] Send data with libwebsockets

Stefano Sabatini stefasab at gmail.com
Mon Oct 27 22:08:52 CET 2014


Hi,

I'm a newbie with libwebsockets and I think I'm doing something wrong
with it.

I have this code in my protocol callback:
 
    case LWS_CALLBACK_RECEIVE:
        printf("Received data: %s\n", (char *)in);
        /* send JPEG compressed image */
        {
#define PAYLOAD_SIZE 4096
            char buf[LWS_SEND_BUFFER_PRE_PADDING + PAYLOAD_SIZE + LWS_SEND_BUFFER_POST_PADDING];
            int n;
            // read data from file
            const char *filename = "lena.jpeg";
            FILE *f = fopen(filename, "r");
            if (!f) {
                fprintf(stderr, "Could not open file '%s'\n", filename);
                break;
            }

            printf("Sending image data...\n");
            while (n = fread(buf + LWS_SEND_BUFFER_PRE_PADDING, 1, PAYLOAD_SIZE, f)) {
                printf("Sending %d bytes...\n", n);
                // memset(&buf[LWS_SEND_BUFFER_PRE_PADDING], 255, PAYLOAD_SIZE);
                libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], n, LWS_WRITE_TEXT);
            }
            fclose(f);
        }
        break;


This causes a failure on the client side, which closes the connection.

Question is: if we have to send data using several segments, what's
the supposed way to do it? Or should I assemble the complete data and
send it with a single call to libwebsocket_write()?

Also sometimes I'm getting this on the server side:
Sending image data...
Sending 4096 bytes...
Sending 4096 bytes...
Sending 4096 bytes...
lwsts[13497]: ****** 7e53e0 Sending new, pending truncated ...
hellowebsockets: /home/stefano/src/libwebsockets/lib/output.c:112: lws_issue_raw: Assertion `0' failed.

Is this a bug, should I fill a bug report for that?

Thanks in advance.



More information about the Libwebsockets mailing list