Ruby – multiple bodies using asynchronous Sinatra

I want to start a very long polling request from javascript, which is good, I want my ruby ​​prog to stream multiple body parts to javascript. Why the following (pseudo) The code does not work?

require'rubygems'
require'sinatra/async'
require'eventmachine'
require'thin'
require'json'

class Test register Sinatra::Async

aget'/process' do
for c in 1 ..10
body {
{:data => ["this is part #{c}"] }.to_json
end
end
end

run!
end

Maybe I have misunderstood what long-term polling and asynchronous should do, but my expectation is that I send multiple agencies back to the client? Do I need to use eventmachine?

Thank you

require'rubygems'
require 'sinatra/async'
require'thin'
require'json'

class Test register Sinatra::Async
< br /> class JSONStream
include EventMachine::Deferrable

def stream(object)
@block.call object.to_json + " "
end

def each(&block)
@block = block
end
end

aget'/process' do
puts'ok '
out = JSONStream.new
body out
EM.next_tick do
c = 0
timer = EM.add_periodic_timer(0.3) do
c + = 1
out.stream :data => ["this is part #{c}"]
if c == 100
timer.cancel
out.succeed
end
end
end
end

run!
end

See also: http://confreaks.net/ videos/564-scotlandruby2011-real-time-rack

I want to start a very long polling request from javascript, which is fine, I want my ruby ​​prog to stream multiple body parts to javascript. Why does the following (pseudo) code not kick in?

require'rubygems'
require'sinatra/async'
require'eventmachine'
require'thin'
require'json'

class Test register Sinatra::Async

aget'/process' do
for c in 1 ..10
body {
{:data => ["this is part #{c}"] }.to_json
end
end
end

run!
end

Maybe I have misunderstood what long-term polling and asynchronous should do, but my expectation is that I send multiple agencies back to the client? Do I need to use eventmachine?

Thank you

require'rubygems'
require'sinatra/async'
require'thin '
require'json'

class Test register Sinatra::Async

class JSONStream
include EventMachine: :Deferrable

def stream(object)
@block.call object.to_json + " "
end

def each(&block)< br /> @block = block
end
end

aget'/process' do
puts'ok'
out = JSONStream.new
body out
EM.next_tick do
c = 0
timer = EM.add_periodic_timer(0.3) do
c += 1
out.stream :data = > ["this is part #{c}"]
if c == 100
timer.cancel
out.succeed
end
end
end
end

run!
end

See also: http://confreaks.net/videos/564-scotlandruby2011-real-time-rack< /p>

Leave a Comment

Your email address will not be published.