Streaming Data

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Streaming Data

Postby jes2850 » Wed May 21, 2014 8:44 pm

I haven't been able to find this yet but someone may have already solved this problem. Is it possible to force ResourceHandler::ReadResponse to get called as some data becomes available instead of having to wait until all of the data is available?

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;
jes2850
Newbie
 
Posts: 1
Joined: Wed May 21, 2014 5:59 pm

Return to Support Forum

Who is online

Users browsing this forum: No registered users and 235 guests