我是Ruby和Sinatra的新手,我正在尝试用它设置一个简单的HTML5 Server-Sent事件,下面的代码在Chrome开发人员版本中运行良好,但在Windows 7和OSX上的Non Developer Builds和Safari都失败了.
浏览器控制台中的错误消息是"无法加载资源:已取消"
var source = new EventSource('pull');
source.addEventListener('message', function(e) {
console.log(e.data);
}, false);
source.addEventListener('open', function(e) {
// Conn open
}, false);
source.addEventListener('error', function(e) {
if (e.eventPhase == EventSource.CLOSED) {
// Connection was closed.
}
}, false);
Run Code Online (Sandbox Code Playgroud)
以下Sinatra路线
get '/pull' do
content_type 'text/event-stream'
newevent = false
response = "data: "+newevent.inspect+" \n\n"
end
Run Code Online (Sandbox Code Playgroud)
我已经尝试过使用JSP和Tomcat的类似服务器端代码,它在所有浏览器上都能正常工作.
关于Sinatra,我需要了解什么?谢谢!
我试图远程运行一些命令,SSH不能进入机器.我想要做的是设置一个运行一些特定命令的Sinatra应用程序,并通过HTTP流输出输出.
示例操作如下所示:
get "/log" do
`tail -f some.log`
end
Run Code Online (Sandbox Code Playgroud)
1据我所知,我需要使用Unicorn(或Mongrel),因为Thin不支持流数据2我认为我需要通过某种IO ruby对象来管道输出命令
我几乎知道怎么做(1)但不知道如何实现(2).
I have a Sinatra app with a long running process (a web scraper). I'd like the app flush the results of the crawler's progress as the crawler is running instead of at the end.
I've considered forking the request and doing something fancy with ajax but this is a really basic one-pager app that really just needs to output a log to a browser as it's happening. Any suggestions?