我目前正在开发一个具有以下功能的网站:
- 实时聊天
- 一些统计数据
- 一些表格
我正在使用:Rails 4 + Puma + Nginx
我正在使用 ActionController::Live 并创建了一个事件操作,它在页面加载时被调用,并绑定(bind)了监听器来监听聊天消息或统计数据的变化。
def events
response.headers["Content-Type"] = "text/event-stream"
sse = ServerEvent.new(response.stream)
redis = Redis.new
# the safe_write method is the implementation of a workaround for the problem stated on
# http://evaleverything.com/2013/09/07/response-streams-with-rails-4-and-redis
sse.safe_write do
redis.psubscribe("redis-foobar-key*") do |on|
on.pmessage do |pattern, event, data|
sse.write(data, { event: event})
end
end
end
rescue IOError
puts "Stream Closed"
ensure
puts "closing all threads and connections\n"
redis.quit
sse.close
end
我面临的问题是:加载页面时,有时一切正常,我可以启动事件源并且事件得到正确处理,但有时事件源请求保持挂起并且不不返回任何类型的错误。
我已经通过以下方式成功尝试:
- chrome+windows
- chrome+macosx(电脑A)
- firefox+macosx(电脑A)
并通过以下方式进行了不成功的尝试:
- chrome+ubuntu(在代理后面)
- firefox+ubuntu(代理后)
- chrome-macosx(计算机 B)
- firefox+macosx(电脑B)
我正在使用 nginx,但我认为这不是问题所在,不过,这是我的配置
upstream bar {
server foo.com:9292;
}
server {
listen 80;
server_name foo.com megafoo.com;
root /(...)/public;
location / {
proxy_pass http://bar;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Connection '';
proxy_http_version 1.1;
chunked_transfer_encoding off;
proxy_buffering off;
proxy_cache off;
if (-f $request_filename) {
break;
}
}
location ~* ^/assets/ {
# Per RFC2616 - 1 year maximum expiry
expires 1y;
add_header Cache-Control public;
# Some browsers still send conditional-GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.
add_header Last-Modified "";
add_header ETag "";
break;
}
}
最佳答案
看起来防病毒软件正在阻止连接,不允许它成功完成
关于ruby-on-rails-4 - EventSource 调用 ActionController::Live 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26530727/