当我的ActionController::Live
监听了大约5分钟后,我的浏览器中会出现以下信息,我的EventSource
断开连接。
EventSource的响应MIME类型为"text/html"而不是"text/event-stream"。终止连接。
你有任何想法这个请求从哪里来的吗?
我正在使用Unicorn和Nginx。
这是我的后端代码:
def listen_to_checkins
response.headers['Content-Type'] = 'text/event-stream'
redis = Redis.new
redis.psubscribe("checkin.*.#{current_branch.id}") do |on|
puts "subscribed..."
on.pmessage do |pattern, event, data|
puts "got message..."
response.stream.write("event: checkin.create\n")
response.stream.write("data: #{data}\n\n")
end
end
# rails 4.2 uses ClientDisconnected instead of IOError
rescue ClientDisconnected
logger.info "Stream closed"
rescue IOError
logger.info "Stream error"
ensure
redis.quit
response.stream.close
end
end
这是我的咖啡:
ready = ->
if !source
source = new EventSource('/validator/listen_to_checkins')
source.addEventListener 'message', (e) ->
console.log "got message"
source.addEventListener 'checkin.create', (e) ->
console.log "got back: #{e.data}"
get_last_checkins()
source.addEventListener 'open', (e) ->
console.log "opened"
source.addEventListener 'error', (e) ->
console.log "error"