I'm working on a proof of concept that is going to require me streaming a large amount of data back to the client and it's a requirement that a progress bar must be shown, which is hard to do if my client isn't receiving anything until all of the data has been generated on the server side.
In the sample below it'd be nice if there was a way to stream the data back to the client as it becomes available instead of waiting until the very end. Any suggestions/tips would be greatly appreciated.
- Code: Select all
void ResponseHandler::_streamTest(CefRefPtr<CefCallback> callback)
{
//Set up the data size and such
std::string sample = "string 0";
_data = new char[sample.size()*10 + 1];
_data[10*sample.size()] = '\0';
_dataSize = sample.size() * 10;
_dataOffset = 0;
//Let's send it 10 strings, incrementing the number each time and waiting 1 second in between
for(int i = 0; i < 10; i++)
{
std::stringstream ss;
ss << "string " << i;
std::string str = ss.str();
memcpy(_data + str.size()*i, &str[0], str.size());
Sleep(1000);
}
_complete = true;
_status = 200;
callback->Continue();
}
- Code: Select all
bool ResponseHandler::ReadResponse(void* dataOut,
int bytesToRead,
int& bytesRead,
CefRefPtr<CefCallback> callback)
{
if(_dataOffset < _dataSize)
{
bytesRead = min(bytesToRead, _dataSize - _dataOffset);
memcpy(dataOut, _data + _dataOffset, bytesRead);
_dataOffset += bytesRead;
return true;
}
return false;
}
Here is my code within ProcessRequest:
- Code: Select all
CefPostTask(TID_FILE, NewCefRunnableMethod(this,
&ResponseHandler::_streamTest, callback));
return true;