[Libwebsockets] Sending from the receive callback

Steve Calfee scalfee at mqidentity.com
Sat Aug 2 02:17:16 CEST 2014


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);
        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);

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?

Thanks, Steve


More information about the Libwebsockets mailing list