我试图在我的应用程序中利用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我开始这样瘦:
@@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的耦合很可能不是问题。
I'm trying to leverage Server-Sent Events in my application. I am using Sinatra and the sinatra-sse gem. This gem wraps the Sinatra stream :keep_alive call.
When running my application on Thin, I have absolutely no problems, and my event stream works as expected. However, when I switched my application to run with Puma everything works, except my sse_stream does absolutely nothing! It just returns a blank page.
My stream is set up like so
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 endI'm starting Thin like so:
@@puma_instance = Puma::Server.new MyApp::WebUI @@puma_instance.add_tcp_listener ip, port @@puma_instance.runAny idea what's going on? Any help would be appreciated.
EDIT: some more info This is what cURL gives when running on 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 $Whereas this is what happens on 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>EDIT: I should add that my application uses EventMachine at its core, so sinatra_sse's coupling to EM is most likely not an issue.
最满意答案
我相信这个问题是围绕着sinatra-sse 明确使用 EventMachine库而展开的,它并没有列为依赖项 。 但是,它在Gemfile中列出了Thin,而EventMachine是Thin的核心依赖项。
Puma的并发模型完全不同 。 事实上,您会在项目自述文件的顶部找到以下声明:
通过允许阻止IO同时运行(基于EventMachine的服务器,如Thin关闭此功能,需要使用特殊库),Puma仍然可以提高MRI的吞吐量。
编辑
如果您有兴趣了解有关Rack,Rails,Puma和SSE的更多信息,您可以通过Ruby / Rails核心成员Aaron Patterson和全能的人员来欣赏这篇伟大的博客文章 。
I believe the issue revolves around sinatra-sse's explicit use of the EventMachine library, which it does not list as a dependency. It does, however, list Thin in its Gemfile and EventMachine is a core dependency of Thin.
Puma's concurrency model is quite different. In fact, you'll find the following statement right at the top of the project's 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).
EDIT
If you're interested in learning more about Rack, Rails, Puma, and SSE, you might enjoy this great blog post by Aaron Patterson, a Ruby/Rails core member and all-around swell guy.
更多推荐
发布评论