我使用libwebsocket在C ++中实现了一个websocket客户端 我想发送大消息,但我将消息有效负载限制为8K,我需要使用该有效负载值 这是我的初始化代码的剪辑:
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;
}
现在的问题是如何发送大于8K的消息 有没有办法缓冲数据,或者我必须使用 fraggle ?
答案 0 :(得分:3)
我找到了解决问题的方法。该问题与用于每个碎片帧的标志和操作码有关 看看这个(libwebsocket)写函数:
int n = libwebsocket_write(wsi, &write_buffer[LWS_SEND_BUFFER_PRE_PADDING], write_len,(libwebsocket_write_protocol)write_mode);
write_mode 应设置如下:
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
如果您需要更多详细信息,请阅读WebSocket RFC的碎片部分: https://tools.ietf.org/html/rfc6455#section-5.4