问题描述:

With Sinatra Streaming is there a way to specify which connections get sent information. For instance only users on a specific account.

I believe this can be done using Eventmachine Channels but I don't know how. Any advice or even better sample code would be appreciated.

Below is the relevant portion of the code. Right now as you can see it just sends to every open connection.

configure do

set server: 'thin', connections: []

set :app_file, __FILE__

set :port, ENV['PORT']

set :public_folder, File.dirname(__FILE__) + '/public'

end

get '/stream', provides: 'text/event-stream' do

stream :keep_open do |out|

settings.connections << out

out.callback { settings.connections.delete(out) }

end

end

post '/update/contact/:id' do

@contact = Contact.get(params[:id])

@contact.update(params[:contact])

settings.connections.each { |out| out << "data: #{@contact.account_id} Contact #{@contact.id} #{@contact.first_name} #{@contact.last_name}\n\n" }

204

end

网友答案:

You can use conditions. In your case you could use the authorization example provided in the Sinatra Readme and have two exact same routes with different authorizations. That way if the first condition fails it will check the second route and should then stream. There might be a one route solution but haven't yet experimented with streaming.

相关阅读:
Top