Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libwebsocket: send big messages with limited payload

I implemented a websocket client in C++ using libwebsocket.
I'd like to send big messages, but I limited the message payload to 8K and I need to use that payload value.
Here it is a snipped of my initialization code:

void
WSManager::initProtocols(void)
{
    memset(protocols, 0, sizeof(protocols));
    protocols[0].name = "default";
    protocols[0].callback = callback;
    protocols[0].per_session_data_size = 1500;
    protocols[0].rx_buffer_size = 8000;

    /* End of the list */
    protocols[1].name = NULL;
    protocols[1].callback = NULL;
    protocols[1].per_session_data_size = 0;
    protocols[1].rx_buffer_size = 0;
}

The problem now is how to send messages greater than 8K.
Is there a way to bufferize data or I have to use fraggle?

like image 402
neoben Avatar asked Oct 19 '22 21:10

neoben


1 Answers

I found the solution to my problem. The issue was related to the flags and the opcode used for each fragmented frame.
Take a look at this (libwebsocket) write function:

int n = libwebsocket_write(wsi, &write_buffer[LWS_SEND_BUFFER_PRE_PADDING], write_len,(libwebsocket_write_protocol)write_mode);

The write_mode should be set as below:

int write_mode;
write_mode = LWS_WRITE_BINARY; // single frame, no fragmentation
write_mode = LWS_WRITE_BINARY | LWS_WRITE_NO_FIN; // first fragment
write_mode = LWS_WRITE_CONTINUATION | LWS_WRITE_NO_FIN; // all middle fragments
write_mode = LWS_WRITE_CONTINUATION; // last fragment

If you need more details, read the fragmentation section of the WebSocket RFC: https://www.rfc-editor.org/rfc/rfc6455#section-5.4

like image 155
neoben Avatar answered Nov 02 '22 08:11

neoben