2

在 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

这是预期的行为吗?

4

0 回答 0