[Libwebsockets] confused regarding multiple protocols

andy at warmcat.com andy at warmcat.com
Sat Jun 6 04:57:22 CEST 2020



On June 5, 2020 10:46:32 PM UTC, Dorian Green <doriangreen97 at gmail.com> wrote:
>I am having trouble understanding how to receive callbacks on protocols
>beyond the first one which is the http protocol right?
>
>static const struct lws_protocols protocols[] = {
>
>        { "http", callback_test_one, 0, 0, },
>        { "connection_status", callback_test_two, 0, 0, },
>        { "client_download", callback_test_three, 0, 0, },
>        { NULL, NULL, 0, 0 }
>
>};
>
>
>Only my LWS_CALLBACK_PROTOCOL_INIT is called for the second and third
>one.
>The other callbacks are never reached.
>
>static int
>callback_test_two(struct lws *wsi, enum lws_callback_reasons
>reason,void *user, void *in, size_t len)
>{
>
>    switch (reason) {
>
>        case LWS_CALLBACK_PROTOCOL_INIT:
>            lwsl_user("%s: (LWS_CALLBACK_PROTOCOL_INIT) testing for
>response\n", __func__);
>            break;
>
>        case LWS_CALLBACK_WSI_CREATE:
>            lwsl_user("%s: (LWS_CALLBACK_WSI_CREATE) testing for
>response\n", __func__);
>            break;
>
>        case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
>            lwsl_user("%s: (LWS_CALLBACK_CLIENT_CONNECTION_ERROR)
>testing for response\n", __func__);
>            break;
>
>        case LWS_CALLBACK_CLIENT_RECEIVE:
>            lwsl_user("%s: (LWS_CALLBACK_CLIENT_RECEIVE) testing for
>response\n", __func__);
>            break;
>
>        case LWS_CALLBACK_CLIENT_ESTABLISHED:
>            lwsl_user("%s: (LWS_CALLBACK_CLIENT_ESTABLISHED) testing
>for response\n", __func__);
>            break;
>
>        case LWS_CALLBACK_CLIENT_WRITEABLE:
>            lwsl_user("%s: (LWS_CALLBACK_CLIENT_WRITEABLE) testing for
>response\n", __func__);
>            break;
>
>        case LWS_CALLBACK_CLIENT_CLOSED:
>            lwsl_user("%s: (LWS_CALLBACK_CLIENT_CLOSED) testing for
>response\n", __func__);
>
>        case LWS_CALLBACK_WSI_DESTROY:
>            lwsl_user("%s: (LWS_CALLBACK_WSI_DESTROY) testing for
>response\n", __func__);
>            break;
>
>        default:
>            break;
>    }
>    return 0;
>}
>
>(The third is identical to this one above.)
>
>I look in the minimal-examples ws-client and ws-server to see if there
>was
>an example. All of those use only one protocol.

The minimal examples are trying to be minimal so you can easily see what goea on... before those came about there were a monster 'test server' that supports multiple protocols, it's still around today in the 'test apps' subdir.

>The first protocol in my list always works fine but the other two never
>progress past the above issue.
>
>Am i using protocols wrong or expecting something other than their
>intended
>use?
>
>If so what is the way to register different callback
>functions/protocols
>for their client/server to utilize?
>
>thank you for your time.

If they are ws protocols, there is a negotiation at ws upgrade time.  The client may send a list of named subprotocols it's willing to use.

https://tools.ietf.org/html/rfc6455#section-1.9

The server picks one it supports and tells the client which one at the end of the upgrade process.  Lws supports both sides of that and binds the connection to the lws_protocols with thw matching name.

-Andy


More information about the Libwebsockets mailing list