我正在尝试在我的应用程序中利用服务器发送的事件。我正在使用 Sinatra 和 sinatra-sse
gem 。这个 gem 包装了 Sinatra stream :keep_alive
调用。
在 Thin 上运行我的应用程序时,我完全没有问题,并且我的事件流按预期工作。但是,当我将我的应用程序切换为与 Puma 一起运行时,一切正常,除了我的 sse_stream
什么都不做!它只返回一个空白页。
我的流是这样设置的
get "/logstream/:server" do
if rbcserver = MyApp.servers[params[:server]]
sse_stream do |stream|
rbcserver.add_web_logger(stream)
stream.callback { rbcserver.remove_web_logger(stream) }
end
else
error 404
end
end
我开始瘦身是这样的:
@@puma_instance = Puma::Server.new MyApp::WebUI
@@puma_instance.add_tcp_listener ip, port
@@puma_instance.run
知道发生了什么事吗?任何帮助将不胜感激。
编辑:更多信息 这是在 Puma 上运行时 cURL 给出的结果
$ curl -L -b cookies.txt -c cookies.txt -i http://localhost:9001/logstream/myserver
HTTP/1.1 200 OK
Content-Type: text/event-stream;charset=utf-8
X-Content-Type-Options: nosniff
Transfer-Encoding: chunked
$
而这就是 Thin 上发生的事情
$ curl -L -b cookies.txt -c cookies.txt -i http://localhost:9001/logstream/myserver
HTTP/1.1 200 OK
Content-Type: text/event-stream;charset=utf-8
X-Content-Type-Options: nosniff
Connection: close
Server: thin 1.5.1 codename Straight Razor
event: <event name>
data: <my data>
event: <event name>
data: <my data>
<continues as more data comes in>
编辑:我应该补充一点,我的应用程序在其核心使用 EventMachine,因此 sinatra_sse
与 EM 的耦合很可能不是问题。
最佳答案
我相信这个问题围绕着 sinatra-sse 的 explicit use。 EventMachine 库,它 does not list as a dependency .但是,它确实在其 Gemfile 中列出了 Thin EventMachine 是 Thin 的核心依赖.
Puma 的并发模型是quite different .事实上,您会在项目的 README 顶部找到以下语句。 :
Puma still improves MRI's throughput by allowing blocking IO to be run concurrently (EventMachine-based servers such as Thin turn off this ability, requiring you to use special libraries).
编辑
如果您有兴趣了解有关 Rack、Rails、Puma 和 SSE 的更多信息,您可能会喜欢 this great blog post作者 Aaron Patterson,他是 Ruby/Rails 的核心成员,也是全能的大佬。
关于ruby - Sinatra 使用 Puma 进行流式传输?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15749327/