[Libwebsockets] Sending from the receive callback

Andy Green andy at warmcat.com
Sat Aug 2 02:41:23 CEST 2014



On 2 August 2014 08:17:16 GMT+08:00, Steve Calfee <scalfee at mqidentity.com> wrote:
>I am doing a very simple protocol. The browser sends a chunk of data,
>the server is supposed to echo back "ok" for each chunk. I am not doing
>anything with the data, just reporting that I got it. The code below is
>from the simple dumb-increment protocol example, with the changes for
>the protocol.
>
>callback_upload_protocol(struct libwebsocket_context *context,
>                            struct libwebsocket *wsi,
>                            enum libwebsocket_callback_reasons reason,
>                            void *user, void *in, size_t len)
>
>    case LWS_CALLBACK_SERVER_WRITEABLE:
>        n = sprintf((char *)p, "ok");
>        lwsl_notice("webwrite %d UPLOAD send= %s\n", n, p);
>
>    m = libwebsocket_write(wsi, (unsigned char *)p, n, LWS_WRITE_TEXT);

Please read the big notice in libwebsockets.h about how to use libwebsocket_write, or just follow what the sample code does to write.

>        lwsl_notice("libwebsockets upload write %s\n", p);
>        if (m < n) {
>            lwsl_err("ERROR %d writing to fl socket\n", n);
>            return -1;
>        }
>        break;
>    case LWS_CALLBACK_RECEIVE:
>           lwsl_notice("libwebsockets upload receive %d bytes\n", len);
>            libwebsocket_callback_on_writable(context, wsi);
>        break;
>
>The problem is if I send a very small chunk of data, around 10K, the
>notice prints that it received the data. But I never get the writeable
>callback.
>
>My main loop spends most of its time idling and calling:
>            (void) libwebsocket_service(context, timer);

Generally that's correct... just follow the code in the samples.

>I guess I don't understand the interaction between the main loop and
>the libwebsockets library. Does my callback have to do something to
>keep reading? Do I have to free anything, like the data the "in" ptr is
>pointing to? What would prevent a socket from becoming writeable?

No you don't have to do anything special other than calling libwebsocket_service() like the samples.

I think it's probably broke because you're not using libwebsocket_write() correctly.

-Andy

>Thanks, Steve
>_______________________________________________
>Libwebsockets mailing list
>Libwebsockets at ml.libwebsockets.org
>http://ml.libwebsockets.org/mailman/listinfo/libwebsockets




More information about the Libwebsockets mailing list