[Libwebsockets] Problem with using multiple sub-protocols

sunny s sunny46916 at gmail.com
Wed Jul 1 16:30:31 CEST 2015


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20150701/90c8ff18/attachment-0001.html>


More information about the Libwebsockets mailing list