0

I'm developing a simple websocket server on Linux using libwebsocket lib.
I need to send an init packet on established event for every client connection.
If I use

memcpy( p, "init", 4);
lws_write(wsi, p, 4, LWS_WRITE_TEXT);

in

case LWS_CALLBACK_ESTABLISHED:

I receive the error

[2018/07/31 18:50:15:5451] ERR: 
[2018/07/31 18:50:15:5451] ERR: 0000: 81 04 69 6E 69 74         ..init
[2018/07/31 18:50:15:5451] ERR: 
[2018/07/31 18:50:15:5451] ERR: ** 0x564b10ce5170: vh: default, prot: lws-minimal, role ws: Illegal back-to-back write of 6 detected...

From the documentation I read that I cannot recall lws_write in this phase but don't understand how to prepare data for the LWS_CALLBACK_SERVER_WRITEABLE phase (and then call lws_callback_on_writable(wsi);..).

Why the error reports 6 bytes when I lws-write 4 bytes?

SteMMo
  • 392
  • 1
  • 4
  • 23
  • I don't use `libwebsockets` myself, but I imagine the `6` might be related to the additional two byte header that's attached to small messages (less than 127 bytes long)... 2 byte header + 4 byte message => 6 bytes written. – Myst Aug 03 '18 at 03:10

1 Answers1

0

According to the documents, lws_write() requires that LWS_PRE bytes are available before the pointer you pass in to write.

char buf[LWS_PRE + 32];                           // buffer of any size plus the LWS_PRE
lws_strncpy(&buf[LWS_PRE], "init", 4);            // use their stncpy to null-terminate
lws_write(wsi, &buf[LWS_PRE], 4, LWS_WRITE_TEXT); // write from your buffer after LWS_PRE bytes.
Moop
  • 3,414
  • 2
  • 23
  • 37