Sha256: 662329cc361fa73a0127835959f67a3761fd198a60caca7a02903635e928115c

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

class WebSocketIO

  def self.port
    @@port ||= 8080
  end

  def self.start(port=8080)
    @@port = port
    EM::defer do
      while !EM::reactor_running? do
        sleep 1
      end
      puts "WebSocketIO.start port:#{port}"
      EM::WebSocket.run :host => "0.0.0.0", :port => 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


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinatra-websocketio-0.0.1 lib/sinatra-websocketio/websocketio.rb