HTML5 Server-Sent Events with Ruby Sinatra

I'm new to Ruby and Sinatra, I'm trying to setup a simple HTML5 Server-Sent Event with it, The code below works fine in Chrome developer builds but fails in Non Developer Builds and Safari on both Windows7 and OSX.

The error message in the browser console is "Failed to load resource: cancelled"

 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);

With the below Sinatra route

get '/pull' do
   content_type 'text/event-stream'
   newevent   = false
   response = "data: "+newevent.inspect+" \n\n"
end

I have tried similar server side code with JSP and Tomcat and it works fine on all browser.

What do I need to know about Sinatra? спасибо!

17
задан Jonas 30 December 2011 в 18:31
поделиться