0
我想SSE功能添加到使用的Redis PubSub的我的服務器應用程序,通過多篇文章指導即: how-to-use-actioncontollerlive-along-with-resque-redis。無法處理斷開連接的客戶SSE
服務器中的Heroku託管,從而心跳是必要的,以及。
...
sse = SSE.new(response.stream)
begin
redis = Redis.new(:url => ENV['REDISCLOUD_URL'])
redis.subscribe(<UUID>, HEARTBEAT_CHANNEL) do |on|
on.message do |channel, data|
begin
if channel == HEARTBEAT_CHANNEL
sse.write('', event: "hb")
else
sse.write(data, event: "offer_update")
end
rescue StandardError => e #I'll call this section- "internal rescue"
puts "Internal: #{$!}"
redis.quit
sse.close
puts "SSE was closed
end
end
end
rescue StandardError => e #I'll call this section- "external rescue"
puts "External: #{$!}"
ensure
redis.quit
sse.close
puts "sse was closed"
end
的問題:
- 我沒有看到任何地方的 「內部救助」 過網談SSE。但是如果由
sse.write
引發,我不會得到哪些人可以發現異常?常見的情況是HB發送時客戶端不再連接,這使得這部分非常關鍵(出現"Internal: client disconnected"
)。我對嗎? - 在這種情況下,將「外部救援」被觸發?客戶端斷開是否導致
sse.write
在「內部塊」(on.message內部)中引發異常?因爲當我試圖模擬它幾十次時,它從未被外部救援所捕獲。 - 此代碼也受到影響。內部營救部分的
redis.quit
引發了另一個由外部營救聲明捕獲的異常:External: undefined method 'disconnect' for #<Redis::SubscribedClient:0x007fa8cd54b820>
。所以 - 它應該怎麼做?我如何才能識別客戶端斷開,以釋放內存&套接字? - 怎麼能夠由
sse.write
引起的異常沒有被外部救援陷入(因爲它應該是從開始),而另一個錯誤(我的第三個問題中所述)已經被抓住?所有這些代碼(outer + inner部分)都在同一個線程中運行,對吧?我很樂意進行深入的解釋。