[Libwebsockets] Problem with using multiple sub-protocols

Roger Light roger at atchoo.org
Wed Jul 1 17:22:46 CEST 2015


Hi Sunny,

I wasn't reading carefully enough, I'd not spotted that you were
creating a client. I'm afraid I've done zero client work with
libwebsockets so can't really comment.

I would suggest using your code with something known working though -
the libwebsockets test servers for example.

Cheers,

Roger



On Wed, Jul 1, 2015 at 3:30 PM, sunny s <sunny46916 at gmail.com> wrote:
> Hello Roger,
>
> Thank you so much for the quick reply.
>
> We had tried keeping "http" at 0th index and dumb-increment-protocol
> at 1st index in "protocols" structure in our client application.
> However, it didn't work.
>
> Please find related code snippet below:
> ====================================================================
> static struct libwebsocket_protocols protocols[] = {
> {
> "http-only",
> callback_http,
> 0
> },
> {
> "dumb-increment-protocol",
> callback_dumb_increment,
> 0
> },
> {
> NULL, NULL, 0
> }
> };
>
> //-------------------------------------------------------------------
> int main(void)
> {
> .......
> .......
>
> context = libwebsocket_create_context(&info);
> /* Note:172.16.10.160 is a server IP
> * Chat is server(C# .NET) service to which client wants to
> * connect. */
>
> client = libwebsocket_client_connect(context,"172.16.10.160",4649,
> 0, "/Chat",
> "172.16.10.160",
> "172.16.10.160",
> protocols[0].name,
> -1);
> while (n>=0)
> n=libwebsocket_service(context,1000);
> }
>
> //-------------------------------------------------------------------
> static int callback_http(struct libwebsocket_context *context,
> struct libwebsocket *wsi,
> enum libwebsocket_callback_reasons reason,
> void *user,void *in, size_t len)
> {
>   return 0;
> }
>
> //-------------------------------------------------------------------
> static int callback_dumb_increment(struct libwebsocket_context *context,
> struct libwebsocket *wsi,
> enum libwebsocket_callback_reasons reason,
> void *user,void *in, size_t len)
> {
> switch (reason)
> {
> case LWS_CALLBACK_CLIENT_ESTABLISHED:
> {
> printf("CONNCETION WITH SERVER ESTABLISHED....\n");
> libwebsocket_callback_on_writable(context,wsi);
> break;
> }
> case LWS_CALLBACK_CLOSED:
> {
> printf(" LWS CALLBACL CLOSED..\n");
> break;
> }
> case LWS_CALLBACK_CLIENT_RECEIVE:
> {
> printf("RECEIVED DATA FROM SERVER:%s\n",(char*)in);
> break;
> }
> case LWS_CALLBACK_CLIENT_WRITEABLE:
> {
> char data[100]="Hello";
> int i=0;
> unsigned char *buff =
> (unsigned char*) malloc (LWS_SEND_BUFFER_PRE_PADDING
> + strlen(data)
> + LWS_SEND_BUFFER_POST_PADDING);
> for(i=0;i<strlen(data);i++)
> buff[LWS_SEND_BUFFER_PRE_PADDING+i]= data[i];
> libwebsocket_write (wsi,&buff[LWS_SEND_BUFFER_PRE_PADDING],
> strlen(data), LWS_WRITE_TEXT);
> free(buff);
> break;
> }
> default:
> printf("unhandled callback\n");
> break;
> }
> return 0;
> }
> ====================================================================
>
> observations:
>
> 1. In client application, after context is created successfully, if
> we use "protocol[1].name" in API call libwebsocket_client_connect()
> then HTTP request & response comes properly. Protocol also switches
> over to websocket but callback “dumb-increment-protocol” does not get
> called. Only “callback_http” gets called.
>
> 2. If we keep dumb-increment-protocol at 0th index of "protocols"
> structure and then try to connect by using "protocol[0].name" then in
> libwebsocket_client_connect() API call, protocol switches over to
> websocket. Callback “dumb-increment-protocol” gets called properly and
> client server communication works successfully.
>
> Are we missing anything in protocols declaration or usage?
>
> Regards,
> Sunny
>
>



More information about the Libwebsockets mailing list