简体   繁体   English

Rails ActionController :: Live连接被删除

[英]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.

相关问题 ActionController :: Live - Rails中的SSE - ActionController::Live - SSE in Rails Rails,ActionController :: Live,Puma:ThreadError - Rails, ActionController::Live, Puma: ThreadError ActionController :: Live是否可以检查连接是否仍然存在? - ActionController::Live Is it possible to check if connection is still alive? Rails ActionController :: Live在开发时无需重启服务器 - Rails ActionController::Live without restart server on developpement 混合redis actioncontroller :: live-Rails应用 - mixing redis actioncontroller::live - rails app Redis订阅+ Rails ActionController :: Live挂起 - Redis Subscription + Rails ActionController::Live hangs 使用`ActiveRecord with_connection do`&ActionController :: Live时出现线程错误 - Threading error when using `ActiveRecord with_connection do` & ActionController::Live Rails ActionController :: Live-一次发送所有内容,而不是异步 - Rails ActionController::Live - Sends everything at once instead of async ActionController :: InvalidAuthenticityToken(ActionController :: InvalidAuthenticityToken):Rails 5 - ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken): Rails 5 Rails 4,Puma,Nginx - ActionController :: Live Streaming在第一个块发送后死亡 - Rails 4, Puma, Nginx - ActionController::Live Streaming dies after first chunk sent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM