Sha256: a6a3e5e749c69810d003c7e3a0b8906d2dd0e2fa3e3a7b4d2051da52a4ce1fc2
Contents?: true
Size: 1.82 KB
Versions: 1
Compression:
Stored size: 1.82 KB
Contents
class WebSocketIO def self.start EM::defer do while !EM::reactor_running? do sleep 1 end puts "WebSocketIO.start port:#{options[:port]}" EM::WebSocket.run :host => "0.0.0.0", :port => options[:port] do |ws| ws.onopen do |handshake| params = parse_handshake_params handshake.path session_id = params[:session] || (create_session Socket.unpack_sockaddr_in ws.get_peername) self.sessions[session_id] = { :websocket => ws } ws.onclose do self.emit :disconnect, session_id end ws.onmessage do |msg| begin data = JSON.parse msg rescue => e self.emit :error, e end self.emit data['type'], data['data'], session_id if data end ws.send({:type => :__session_id, :data => session_id}.to_json) self.emit :connect, session_id end end end end def self.push(type, data, opt={}) session_ids = opt[:to].to_s.empty? ? self.sessions.keys : [opt[:to]] session_ids.each do |id| s = sessions[id] begin s[:websocket].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 } } end def self.create_session(ip_addr) Digest::MD5.hexdigest "#{Time.now.to_i}_#{Time.now.usec}_#{ip_addr}" end def self.parse_handshake_params(path) tmp = path.gsub(/^\//,'').split(/\=+/) params = {} while !tmp.empty? params[tmp.shift.to_sym] = tmp.shift end params end end EventEmitter.apply WebSocketIO WebSocketIO.on :__session_id do |data, from| WebSocketIO.push :__session_id, from, :to => from end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
sinatra-websocketio-0.0.3 | lib/sinatra-websocketio/websocketio.rb |