나는 다음과 같은시나 응용 프로그램이 있습니다sinatra 스트리밍 API를 사용하여 redis 연결을 닫으려면 어떻게해야합니까?
require 'sinatra'
require 'redis'
require 'json'
class FeedStream < Sinatra::Application
helpers do
include SessionsHelper
def redis
@redis ||= Redis.connect
end
end
get '/feed', provides: 'text/event-stream' do
stream do |out|
redis.subscribe "feed" do |on|
on.message do |channel, message|
event_data = JSON.parse message
logger.info "received event = #{event_data}"
out << "event: #{event_data['event']}\n"
out << "data: #{{:data => event_data['data'],
:by => current_user}}.to_json\n\n"
end
end
end
end
end
기본적으로, 그것은 레디 스 pubsub를 사용하여 피드를 다른 사용자가 게시 된 이벤트를 수신 한 다음이시나 스트리밍 API를 사용하여 해당 이벤트를 보냅니다. 브라우저가 피드에 다시 연결될 때, redis 클라이언트는 계속 연결된 상태를 유지하고 계속 이벤트를 수신하므로 redis 서버는 쓸모없는 연결로 가득 차게됩니다. broser가 웹 서버에 대한 연결을 닫으면이 연결을 어떻게 닫을 수 있습니까?
이것을 알아낼 수 있었습니까? 레일에서 비슷한 문제가 발생하여 [유사한 질문을 게시했습니다] (http://stackoverflow.com/q/18970458/877472). 문제는 잠재적 인 해결책을 포함하고 있습니다. –