我很難搞清楚如何使用Rails來做到這一點,儘管這可能是因爲我不知道它的適當術語。如何在Rails中長時間運行的同步請求中生成響應?
我基本上要做到這一點:
def my_action
sleep 1
# output something in the request, but keep it open
print '{"progress":15}'
sleep 3
# output something else, keep it open
print '{"progress":65}'
sleep 1
# append some more, and close the request
print '{"sucess":true}'
end
但我無法弄清楚如何做到這一點。我基本上想複製一個緩慢的互聯網連接。
我需要這樣做,因爲我在刮網站,這需要時間,我在上面「睡覺」。
更新 我正在閱讀這個使用iOS,所以我不想要一個websocket服務器,我想。
你可能術語是「沖洗」之後。您可能希望它在每次打印後刷新輸出()。有一些關於Rails 3.1自動刷新的信息http://yehudakatz.com/2010/09/07/automatic-flushing-the-rails-3-1-plan/雖然沒有直接關係。 – mahemoff 2012-01-01 14:29:19