Module: Sinatra::RocketIO
- Defined in:
- lib/sinatra/rocketio.rb,
lib/sinatra/rocketio/client.rb,
lib/sinatra-rocketio/channel.rb,
lib/sinatra-rocketio/helpers.rb,
lib/sinatra-rocketio/options.rb,
lib/sinatra-rocketio/version.rb,
lib/sinatra-rocketio/rocketio.rb,
lib/sinatra-rocketio/javascript.rb,
lib/sinatra-rocketio/application.rb,
lib/sinatra-rocketio/client_info.rb
Defined Under Namespace
Modules: Helpers Classes: Client, ClientInfo
Constant Summary collapse
- VERSION =
"0.3.2"
Class Method Summary collapse
- .channels ⇒ Object
- .default_options ⇒ Object
- .javascript(*js_file_names) ⇒ Object
- .options ⇒ Object
- .options=(opts) ⇒ Object
- .push(type, data, opt = {}) ⇒ Object
- .registered(app) ⇒ Object
- .sessions ⇒ Object
Instance Method Summary collapse
Class Method Details
.channels ⇒ Object
4 5 6 |
# File 'lib/sinatra-rocketio/channel.rb', line 4 def self.channels @@channels ||= {} end |
.default_options ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/sinatra-rocketio/options.rb', line 12 def self. { :comet => [ (ENV["COMET"].to_s =~ /^(false|disable)$/i ? false : true), lambda{|v| [true, false].include? v } ], :websocket => [ (ENV["WEBSOCKET"].to_s =~ /^(false|disable)$/i ? false : true), lambda{|v| [true, false].include? v } ] } end |
.javascript(*js_file_names) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/sinatra-rocketio/javascript.rb', line 4 def self.javascript(*js_file_names) js_file_names = ['rocketio.js', 'cometio.js', 'websocketio.js', 'event_emitter.js'] js = '' js_file_names.each do |i| js += case i when 'cometio.js' [:comet] ? Sinatra::CometIO.javascript('cometio.js') : '' when 'websocketio.js' [:websocket] ? Sinatra::WebSocketIO.javascript('websocketio.js') : '' else j = '' File.open(File. "../js/#{i}", File.dirname(__FILE__)) do |f| j = f.read end j end + "\n" end js end |
.options ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/sinatra-rocketio/options.rb', line 25 def self. @@options ||= ( opts = {} .each do |k,v| opts[k] = v[0] end opts ) end |
.options=(opts) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sinatra-rocketio/options.rb', line 35 def self.(opts) @@options = {} opts.each do |k,v| k = k.to_sym unless .include? k STDERR.puts "!! Sinatra::RocketIO setting - \"#{k}\" is not valid key" else unless [k][1].call(v) default = [k][0] STDERR.puts "!! Sinatra::RocketIO setting - \"#{k} => #{v}\" is not valid. set default \"#{k} => #{default}\"" @@options[k] = default else @@options[k] = v end end end .each do |k, v| @@options[k] = v[0] unless @@options.include? k end @@options end |
.push(type, data, opt = {}) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/sinatra-rocketio/rocketio.rb', line 6 def self.push(type, data, opt={}) if opt.include? :to and opt[:to].kind_of? Array opt[:to].each do |to| push type, data, :to => to end elsif opt.include? :channel channels.select{|session, channel| channel == opt[:channel].to_s }.each{|session, channel| push type, data, :to => session } else if [:websocket] Sinatra::WebSocketIO.push type, data, opt end if [:comet] Sinatra::CometIO.push type, data, opt end end end |
.registered(app) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/sinatra-rocketio/application.rb', line 4 def self.registered(app) app.register Sinatra::CometIO app.register Sinatra::WebSocketIO app.helpers Sinatra::RocketIO::Helpers app.get '/rocketio/settings' do content_type 'application/json' response["Access-Control-Allow-Origin"] = "*" @setting_json ||= ( setting = {} setting[:websocket] = websocketio_url if Sinatra::RocketIO.[:websocket] setting[:comet] = cometio_url if Sinatra::RocketIO.[:comet] setting.to_json ) end app.get '/rocketio/rocketio.js' do content_type 'application/javascript' @js ||= ERB.new(Sinatra::RocketIO.javascript).result(binding) end EM::defer do while !EM::reactor_running? do sleep 1 end Sinatra::WebSocketIO.start if Sinatra::RocketIO.[:websocket] Sinatra::RocketIO.emit :start end end |