我是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,我需要了解什么?谢谢!