我正在開發一個HTTPS
服務器,它接收到一個請求並且必須回答3個響應。前兩個是線路確認,最後一個包含請求的信息。從boost asio發送多個響應給該瀏覽器
我正在使用我的網絡瀏覽器(chrome)作爲客戶端。我想要的是以下內容:
- 瀏覽器(客戶端)向服務器發送請求。
- 服務器發送第一個ACK(一個html頁面),瀏覽器顯示它。
- 兩秒鐘後,服務器發送另一個ACK(一個不同的html頁面),瀏覽器顯示它。
- 再過兩秒鐘後,服務器發送請求的信息(不同的html頁面),瀏覽器顯示它。
的問題是,瀏覽器只接收第一ACK,似乎它是看完後關閉套接字,甚至在HTTPS
頭中的Connection
設置爲keep-alive
。
有什麼辦法可以等待幾個HTTPS
響應與網頁瀏覽器?
來源
這包含由服務器執行的異步方法的時候了一份請願書是由:
void handle_handshake(const boost::system::error_code& error)
{
if (!error)
{
boost::asio::async_read_until(socket_, request_, "\r\n\r\n",
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error));
}
else
{
std::cout << "ERROR, deleting. " << __FILE__ << ":" << __LINE__ << std::endl;
delete this;
}
}
void handle_read(const boost::system::error_code& err)
{
if (!err)
{
std::string s = "some_response";
// First write. This write is received by the browser without problems.
boost::asio::async_write(socket_,
boost::asio::buffer(response),
boost::bind(&session::handle_write, this,
boost::asio::placeholders::error));
}
else
{
std::cout << "Error: " << err << "\n";
}
}
void handle_write(const boost::system::error_code& error)
{
if (!error)
{
if(n++ <= 2)
{
// Second and third writes.
// These ones are not read by the browser.
if(n == 1)
{
std::string s = "some_response2";
boost::asio::async_write(socket_,
boost::asio::buffer(response),
boost::bind(&session::handle_write, this,
boost::asio::placeholders::error));
}
else if (n==2)
{
std::string s = "some_response3";
boost::asio::async_write(socket_,
boost::asio::buffer(response),
boost::bind(&session::handle_write, this,
boost::asio::placeholders::error));
}
sleep(1);
}
}
else
{
std::cout << "ERROR, deleting: " << __FILE__ << ":" << __LINE__ << std::endl;
delete this;
}
}
你能不能顯示代碼片段? –