-
-
Save Epictetus/3020293 to your computer and use it in GitHub Desktop.
Rails 3.1 Sinatra streaming question...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'thin' | |
class Metaserver < Sinatra::Base | |
set server: 'thin' | |
set connections: [] | |
get '/stream', provides: 'text/event-stream' do | |
stream :keep_open do |out| | |
settings.connections << out | |
out.callback { settings.connections.delete(out) } | |
out.errback { settings.connections.delete out } | |
end | |
end | |
post '/stream' do | |
puts "Relay to #{settings.connections.count} streams" | |
settings.connections.each { |out| out << "data: #{params[:msg]}\n\n" } | |
204 # response without entity body | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'metaserver' | |
Metaverse::Application.routes.draw do | |
match '/stream', :to => Metaserver | |
resource :login, :only => :show | |
resource :explore, :only => :show, :controller => 'explore' | |
root :to => 'login#show' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment