本文介绍了如何通过libwebsocket发送异步数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用warmcat的libwebsocket C库的一个小的WebSocket服务器。我有实施例和工作,并可以响应数据发送到从的WebSocket接收数据(如回显发送的反向字节)。不过,我一直无法弄清楚如何从服务器异步发送数据,而无需在libwebsocket回调函数已经被。我需要的WSI指针传递给libwebsocket_write(),但不知道怎样得到的指针除,而在回调。

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.

我读过libwebsocket不是线程安全的,因为我自己的经验似乎印证。

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

在libwebsockets.c:line2483,我读到协议回调函数[...]所谓周期性允许异步传输。这正是我想要的,但我并没有在自己的code观察到这种情况的发生,并没有发现任何方式打开它。

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".

这是如何异步写我服务于WebSocket的建议?

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

code为libwebsocket:http://git.warmcat.com/cgi-bin/cgit/libwebsockets/

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

使用示例:http://martinsikora.com/libwebsockets-simple-websocket-server

推荐答案

我还没有发现这样做的一个超级干净的方式。我的建议是:在服务器可以写入到客户端,然后检查是否有异步工作方式发送有事件注册一个回调。在 LWS_CALLBACK_SERVER_WRITEABLE 事件就是这样的,如果你调用 libwebsocket_callback_on_writable(背景下,WSI); 从你的回调中它会定期调用。

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.

所以,这样的事情:

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

我适应这个从测试fraggle.c 例子;上面大致什么例子并在较小的块发送消息。

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

这篇关于如何通过libwebsocket发送异步数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 13:16
查看更多