[Libwebsockets] Sending from the receive callback

Steve Calfee scalfee at mqidentity.com
Mon Aug 4 19:34:26 CEST 2014


I found the problem in the browser javascript. I was not waiting for the open to finish and the sends to finish before going to the next state, so the client socket got closed before the server could do its job.

Thanks for trying to help
________________________________________
From: Andy Green <extracats at googlemail.com> on behalf of andy.green at linaro.org <andy.green at linaro.org>
Sent: Saturday, August 02, 2014 10:51 AM
To: libwebsockets at ml.libwebsockets.org
Subject: Re: [Libwebsockets] Sending from the receive callback

What is p pointing to?  Is it a buffer that contains space for padding?
You have to make sure you include padding as in the example code.

-m

On 8/1/2014 8:17 PM, Steve Calfee wrote:
> I am doing a very simple protocol. The browser sends a chunk of data, the server is supposed to echo back "ok" for each chunk. I am not doing anything with the data, just reporting that I got it. The code below is from the simple dumb-increment protocol example, with the changes for the protocol.
>
> callback_upload_protocol(struct libwebsocket_context *context,
>                              struct libwebsocket *wsi,
>                              enum libwebsocket_callback_reasons reason,
>                              void *user, void *in, size_t len)
>
>      case LWS_CALLBACK_SERVER_WRITEABLE:
>          n = sprintf((char *)p, "ok");
>          lwsl_notice("webwrite %d UPLOAD send= %s\n", n, p);
>
>          m = libwebsocket_write(wsi, (unsigned char *)p, n, LWS_WRITE_TEXT);
>          lwsl_notice("libwebsockets upload write %s\n", p);
>          if (m < n) {
>              lwsl_err("ERROR %d writing to fl socket\n", n);max transfer size
>              return -1;
>          }max transfer size
>          break;
>      case LWS_CALLBACK_RECEIVE:
>              lwsl_notice("libwebsockets upload receive %d bytes\n", len);
>              libwebsocket_callback_on_writable(context, wsi);
>          break;max transfer size
>
> The problem is if I send a very small chunk of data, around 10K, the notice prints that it received the data. But I never get the writeable callback.
>
> My main loop spends most of its time idling and calling:max transfer size
>              (void) libwebsocket_service(context, timer);
>
> I guess I don't understand the interaction between the main loop and the libwebsockets library. Does my callback have to do something to keep reading? Do I have to free anything, like the data the "in" ptr is pointing to? What would prevent a socket from becoming writeable?
>
> Thanks, Steve
> _______________________________________________max transfer size
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> http://ml.libwebsockets.org/mailman/listinfo/libwebsockets

_______________________________________________
Libwebsockets mailing list
Libwebsockets at ml.libwebsockets.org
http://ml.libwebsockets.org/mailman/listinfo/libwebsockets



More information about the Libwebsockets mailing list