lib/sinatra-websocketio/websocketio.rb in sinatra-websocketio-0.1.4 vs lib/sinatra-websocketio/websocketio.rb in sinatra-websocketio-0.1.5

- old
+ new

@@ -22,13 +22,11 @@ if self.sessions.include? session_id ws.send({:type => :error, :data => "invalid session_id (#{session_id})"}.to_json) ws.close else - self.sessions[session_id] = { - :websocket => ws - } + self.sessions[session_id] = ws ws.onclose do self.sessions.delete session_id self.emit :disconnect, session_id end ws.onmessage do |msg| @@ -49,24 +47,21 @@ end def self.push(type, data, opt={}) session_ids = opt[:to].to_s.empty? ? self.sessions.keys : [opt[:to]] session_ids.each do |id| + next unless sessions.include? id s = sessions[id] begin - s[:websocket].send({:type => type, :data => data}.to_json) + s.send({:type => type, :data => data}.to_json) rescue next end end end def self.sessions - @@sessions ||= Hash.new{|h,session_id| - h[session_id] = { - :websocket => nil - } - } + @@sessions ||= Hash.new end def self.create_session(ip_addr) Digest::MD5.hexdigest "#{Time.now.to_i}_#{Time.now.usec}_#{ip_addr}" end