Rails ActionController :: Live连接被删除

时间:2015-10-12 22:42:39

标签: ruby-on-rails ruby-on-rails-4

ActionController::Live听了大约5分钟之后,我在浏览器中收到以下内容,EventSource删除了连接。

EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection.

知道'text / html'请求来自哪里?

我正在使用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"

1 个答案:

答案 0 :(得分:0)

好的..我最终在客户端重新连接错误。我希望这能有所帮助,直到找到更优雅的解决方案。

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