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>

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3284 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.