[Libwebsockets] LWS_CALLBACK_HTTP send/write text only

Peter Kroon plakroon at gmail.com
Thu Apr 9 15:24:23 CEST 2015


Yes, I've got it working with the code below.
You have to set the headers manually.

char the_response[] = "Hello, World!";
int strl = strlen(the_response);

unsigned char *p;
unsigned char *end;
static unsigned char buffer[4096];

p = buffer + LWS_SEND_BUFFER_PRE_PADDING;
end = p + sizeof(buffer) - LWS_SEND_BUFFER_PRE_PADDING;

if (lws_add_http_header_status(context, wsi, 200, &p, end))
  return 1;
if (lws_add_http_header_by_token(context, wsi,
    WSI_TOKEN_HTTP_SERVER,
        (unsigned char *)"libwebsockets",
    13, &p, end))
  return 1;
if (lws_add_http_header_by_token(context, wsi,
    WSI_TOKEN_HTTP_CONTENT_TYPE,
        (unsigned char *)"text/plain",
    10, &p, end))
  return 1;
if (lws_add_http_header_content_length(context, wsi,
      strl, &p, end))
  return 1;
if (lws_finalize_http_header(context, wsi, &p, end))
  return 1;

libwebsocket_write(wsi,
                    buffer + LWS_SEND_BUFFER_PRE_PADDING,
                    p - (buffer + LWS_SEND_BUFFER_PRE_PADDING),
                    LWS_WRITE_HTTP_HEADERS);

int n = libwebsocket_write(wsi, the_response, strl, LWS_WRITE_HTTP);
printf("printing hello world ?....%d\n", n);






2015-04-08 13:22 GMT+02:00 Peter Kroon <plakroon at gmail.com>:

> For some reason I'm not getting any emails send to the list. Checked
> settings, look OK.
> I've read your reply on:
> http://ml.libwebsockets.org/pipermail/libwebsockets/2015-April/thread.html
>
> >How about it is working properly, but the browser is waiting for some
> legit http / html to appear befoe rendering it?  When the socket closes he
> gives up.
> >For example it should be saying "200 OK\r\n"...
>
> I've tried it with:
> the_response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nHello,
> World!"; and the result is the same.
> Do you have any other suggestions?
>
> Regards,
> Peter
>
>
> 2015-04-07 19:49 GMT+02:00 Peter Kroon <plakroon at gmail.com>:
>
>> Hi,
>>
>> I'm trying to create a http server that will send some text to all
>> requests.
>> I''ve used the code below. Only when I do Ctrl-C the webserver is
>> shutdown and the text will appear on the page in the webbrowser. Any hints
>> on how I can make this work properly?
>>
>>
>> case LWS_CALLBACK_HTTP: {
>>   char the_response[] = "Hello, World!";
>>   int strl = strlen(the_response);
>>   int n = libwebsocket_write(wsi, the_response, strl, LWS_WRITE_HTTP);
>>   printf("writing hello world ?....%d\n", n);
>> }
>>
>>
>> Thanks,
>> Peter
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20150409/6c345ca1/attachment-0001.html>


More information about the Libwebsockets mailing list