Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send async data via libwebsocket?

Tags:

c

websocket

I am using warmcat's libwebsocket C library for a small websocket server. I have the examples up and working and can send data in response to receiving data from the websocket (such as echoing back the reversed bytes sent). However, I haven't been able to figure out how to send data asynchronously from the server without already being in the libwebsocket callback function. I need the wsi pointer to pass to libwebsocket_write() but don't see how to get the pointer except while in the callback.

I've read that libwebsocket is not thread safe, as my own experience seems to confirm.

In libwebsockets.c:line2483, I read that "The protocol callback functions are [...] called periodically to allow async transmission." This is exactly what I want but I haven't observed this happening in my own code and haven't found any way to "turn it on".

Suggestions on how to write asynchronously to the websocket I am serving?

Code for libwebsocket: http://git.warmcat.com/cgi-bin/cgit/libwebsockets/

Example of use: http://martinsikora.com/libwebsockets-simple-websocket-server

like image 536
user1967117 Avatar asked Jan 10 '13 15:01

user1967117


1 Answers

I haven't found a super clean way of doing that. What I would suggest is to register a callback on the event that server can write to the client and then check if there's asynchronous work to send there. the LWS_CALLBACK_SERVER_WRITEABLE event is just that, and if you call libwebsocket_callback_on_writable(context, wsi); from within your callback it'll be periodically called.

So, something like this:

static int callback_myprotocol(struct libwebsocket_context *context,
                               struct libwebsocket *wsi,
                               enum libwebsocket_callback_reasons reason,
                               void *user, void *in, size_t len)
{
    SendState *ss = (SendState*)user;

    switch (reason) {
    case LWS_CALLBACK_ESTABLISHED: 
        printf("connection established\n");
        // get the ball rolling
        libwebsocket_callback_on_writable(context, wsi);
        break;

    case LWS_CALLBACK_SERVER_WRITEABLE: {
        if (!work_to_be_done) {
            // schedule ourselves to run next tick anyway
            libwebsocket_callback_on_writable(context, wsi);
            return 0;
        }
        // send our asynchronous message
        libwebsocket_write(wsi, buf, size, flag);
        // and schedule ourselves again
        libwebsocket_callback_on_writable(context, wsi);
        break;
    }
    default:
        break;
    }

    return 0;
}

I adapted this from the test-fraggle.c example; the above is roughly what that example does to send messages in smaller chunks.

like image 60
Henrik Abelsson Avatar answered Oct 29 '22 17:10

Henrik Abelsson