I'm currently sending rapidjson::Value of array type called databuf to a websocket from boost library.
Here is how I load databuf at each loop.
rapidjson::Value databuf(kArrayType);
databuf.SetArray();
for (size_t j = 0; j < sizeof(pu8resbuf); j )
{
if(databuf.IsNull() == true)
{
printf("databuf is null!\n");
}
databuf.PushBack(Value().SetInt(pu8resbuf[j]),allocator);
}
Then, I pushback the databuf to another rapidjson::value of type array called payload and prepare the string to send it to the websocket as below.
payload.SetArray();
payload.PushBack(databuf, allocator);
auto raw_key = std::string(std::string("payload-") std::to_string(m_count_objects/60) "-" std::to_string(m_count_objects).c_str());
rapidjson::Value key(raw_key, allocator);
rapidjson::StringBuffer bufferJson;
jsonDocumentDataSending.AddMember(key, payload, allocator);
bufferJson.Clear();
rapidjson::Writer<rapidjson::StringBuffer> writer(bufferJson);
jsonDocumentDataSending.Accept(writer);
std::string stringForSending = std::string(bufferJson.GetString());
std::shared_ptr<std::string> ss(std::make_shared<std::string>(stringForSending));
messageQ.push_back(ss);
if(!messageQ.empty())
{
ws_.write(net::buffer(*messageQ.front()));
messageQ.pop_back();
databuf.SetArray();
}
The following is the result I've got on the frontend regarding the incoming packets from the websocket.
As you can see the packets sent are getting longer at each loops as if the sending buffer is not resetting for some reason.
Does anyone know what to fix in my code?
CodePudding user response:
I've reset the writer and clear the bufferJson as well like below but didn't work.
bufferJson.Clear();
bufferJson.Flush();
rapidjson::PrettyWriter<rapidjson::StringBuffer>
writer(bufferJson);
writer.Reset(bufferJson);
jsonDocumentDataSending.Accept(writer);
What worked for me was swapping the document after sending the packet to websocket, and I did it as below.
if(!messageQ.empty())
{
ws_.write(net::buffer(*messageQ.front()));
messageQ.pop_back();
databuf.SetArray();
Value(kObjectType).Swap(jsonDocumentDataSending);
}
CodePudding user response:
You need to reset the writer.