2

All the examples found on warmcat/libwebsockets are all about accepting http requests and serving webpage/files. I am trying to do a websocket connection, ex: ws://123.0.0.1/blah however it won't ever establish the connection properly.

I am using curl to try to communicate with my websocket server:

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

On my server, I am not doing anything special, just trying to print out what's the flow.

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

In my main(), I run libev to poll:

ev_timer_init(t, request_polling_cb, 1, 1);

Update:

I've also tried a different curl command like so:

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

And I see this:

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

Is this the expected behavior?

codenamezero
  • 2,724
  • 29
  • 64
  • Did you look over [this example](https://github.com/warmcat/libwebsockets/tree/master/minimal-examples/ws-server/minimal-ws-server)? Also... as I side note, does it have to be `libwebsockets`? What made you choose `libwebsockets`? – Myst Mar 19 '18 at 22:15
  • Yes I've seen that example but i'm using `libev`. I'm supposedly to simply accept the connection from the websocket, but I couldn't find any example on accepting the connection from the callback. – codenamezero Mar 20 '18 at 11:55
  • Maybe the `libev` tag would help your question reach people that could help you. It seems to me that this might be a `libev` integration question rather than a `libwebsocket` API question. P.S. I don't know much about `libev`, but I'm pretty sure that `ev_timer_init` isn't what you're looking for. Evented programming usually doesn't use timers all that much or even at all (maybe I'm wrong, I'm the author for facil.io, so I don't use `libev` all that much). – Myst Mar 21 '18 at 04:08
  • I did tried the `minimal-ws-server` example without using `libev` and is still the same. Testing it with curl just get `Empty reply from server`. – codenamezero Mar 21 '18 at 13:28
  • What do you expect `curl` to return? Also, assuming the headers should emulate an upgrade request, [the request is missing some required headers](https://tools.ietf.org/html/rfc6455#page-21) – Myst Mar 21 '18 at 20:34
  • Actually, that's what I want to find out as well, I don't even know what is the expected outcome when a `curl` websocket request is properly accepted. Please see my updates on my question. – codenamezero Mar 22 '18 at 14:43

0 Answers0