2
我正在開發一個在服務器端使用Sinatra的API。我想要發出一個HTTP請求,但會繼續掛起/等待並保持活動狀態,直到稍後的事件(另一個事件)導致它在稍後時間以特定的響應值完成爲止。如何等待並稍後發送對HTTP請求的響應?
換句話說:
get '/api/foo/:request_identifier' do
# some code here
wait_until_finished params[:request_identifier]
end
# When this URL is visited, the hanging request with the matching
# request identifier will complete, sending "foo response text" to the
# client.
get '/api/bar/:request_identifier' do
make_it_finish params[:request_identifier] "foo response text"
"bar response text"
end
我怎麼能實現這一點,還是有得這種效果呢?
我也考慮讓客戶端不斷向服務器輪詢已完成請求的請求,但大量的請求可能會導致昂貴的互聯網帳單。