Ily*_* O. 9 ruby rack thin sinatra puma
我正在尝试在我的应用程序中利用Server-Sent Events.我正在使用Sinatra和sinatra-sse宝石.这个宝石包裹着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
Run Code Online (Sandbox Code Playgroud)
我这样开始瘦:
@@puma_instance = Puma::Server.new MyApp::WebUI
@@puma_instance.add_tcp_listener ip, port
@@puma_instance.run
Run Code Online (Sandbox Code Playgroud)
知道发生了什么事吗?任何帮助,将不胜感激.
编辑:更多信息这是cURL在Puma上运行时提供的
$ 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
$
Run Code Online (Sandbox Code Playgroud)
而这就是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>
Run Code Online (Sandbox Code Playgroud)
编辑:我应该补充一点,我的应用程序使用EventMachine作为核心,因此sinatra_sse与EM的耦合很可能不是问题.
我相信这个问题围绕着 sinatra-sse对 EventMachine 库的显式使用,它没有将其列为依赖项。但是,它确实在其Gemfile中列出了 Thin ,并且 EventMachine 是Thin的核心依赖项。
Puma 的并发模型有很大不同。事实上,您会在项目的顶部找到以下语句事实上,您会在项目README:
Puma 仍然通过允许并发运行阻塞 IO 来提高 MRI 的吞吐量(基于 EventMachine 的服务器,例如 Thin 关闭此功能,需要使用特殊的库)。
编辑
如果您有兴趣了解有关 Rack、Rails、Puma 和 SSE 的更多信息,您可能会喜欢Aaron Patterson(Ruby/Rails 核心成员和全能专家)撰写的这篇精彩博客文章。