你如何接受与libwebsockets的websocket连接?

时间:2018-03-19 20:27:43

标签: c web-services websocket libwebsockets libev

在warmcat / libwebsockets上找到的所有示例都是关于接受http请求和提供网页/文件。我正在尝试做一个websocket连接,例如:ws://123.0.0.1/blah但是它无法正常建立连接。

我正在使用curl尝试与我的websocket服务器进行通信:

curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: 123.0.0.1" -H "Origin: http://123.0.0.1" http://123.0.0.1
[ DEBUG] Reason 17
[ DEBUG] Reason 29
[ DEBUG] Reason 35
[ DEBUG] Reason 32
[ DEBUG] Reason 36
[ DEBUG] Reason 19
[ DEBUG] Reason 34
[ DEBUG] Reason 20
[ DEBUG] Reason 35
[ DEBUG] Reason 34
[ DEBUG] Reason 36
curl: (52) Empty reply from server

在我的服务器上,我没有做任何特别的事情,只是试图打印出流量的内容。

enum supported_protocols {
    PROTOCOL_HTTP = 0,
    PROTOCOL_COUNT
};

static struct lws_protocols protocols[] =
{
    {
        "PROTOCOL_HTTP",
        my_callback,
        0,
        4096,
    },
    { NULL, NULL, 0, 0 } /* terminator */
};

static void
request_polling_cb (EV_P_ ev_timer *w, int revents)
{
    lws_callback_on_writable_all_protocol(context,
                    &protocols[PROTOCOL_HTTP]);
}

static int
my_callback(struct lws *wsi,
                 enum lws_callback_reasons reason,
                 void *user, void *in, size_t len)
{

    int ret = 0;
    switch(reason) {
        case LWS_CALLBACK_ESTABLISHED:
           DBG("ESTABLISHED (%d)\n", reason);
           break;
        case LWS_CALLBACK_SERVER_WRITEABLE:
            DBG("SERVER_WRITEABLE (%d)\n", reason);
            break;
        case LWS_CALLBACK_HTTP:
            DBG("LWS_CALLBACK_HTTP\n");
            break;
        case LWS_CALLBACK_CLOSED:
            DBG("LWS_CALLBACK_CLOSED\n");
            break;
        default:
            DBG("Reason %d\n", reason);
            break;
    }

    return ret;
}

在我的main()中,我运行libev进行民意调查:

ev_timer_init(t, request_polling_cb, 1, 1);

更新

我也尝试了不同的curl命令:

curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: localhost" -H "Origin: http://localhost" -H "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" -H "Sec-WebSocket-Versi on: 13" http://localhost:7681

我明白这一点:

HTTP/1.1 101 Switching Protocols
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Accept: qGEgH3En71di5rrssAZTmtRTyFk=

^C[2018/03/21 09:58:15:3809] NOTICE: lws_service_fd_tsi: zero length read

这是预期的行为吗?

0 个答案:

没有答案