[英]Rails ActionController::Live connection dropped
After my ActionController::Live
listens for about 5 minutes, I get the following in my browser and my EventSource
drops the connection. 在我的ActionController::Live
监听大约5分钟后,我在浏览器中得到以下内容,而我的EventSource
丢弃了连接。
EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection.
Any idea where that 'text/html' request is coming from? 知道'text / html'请求来自何处?
I'm using Unicorn and Nginx. 我正在使用Unicorn和Nginx。
This is my backend code: 这是我的后端代码:
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
And this is my coffee: 这是我的咖啡:
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"
Okay.. I ended up reconnecting on error on the client's side. 好的..我最终在客户端重新连接错误。 I hope this helps someone until I find a more elegant solution. 我希望这能有所帮助,直到找到更优雅的解决方案。
ready = ->
if !source
reconnect()
reconnect = ->
if ($("meta[name=listen_to_checkins]").attr("content") == "true")
console.log("reconnecting")
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}"
show_checkins(JSON.parse(e.data))
source.addEventListener 'error', (e) ->
# reload page on error if we're listening to checkins
setTimeout ()->
reconnect()
, 1000
console.log "error"
console.log e
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.