Sha256: 52890ab5a803ea416469ff212b20ec531d5a0994948a16cbcb6d2f8549ac5007

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

module Faye
  class WebSocket
    
    class Client
      include API
      attr_reader :uri
      
      def initialize(url)
        @parser = Protocol8Parser.new(self, :masking => true)
        @url    = url
        @uri    = URI.parse(url)
        
        @ready_state = CONNECTING
        @buffered_amount = 0
        
        port = @uri.port || (@uri.scheme == 'wss' ? 443 : 80)
        
        EventMachine.connect(@uri.host, port, Connection) do |conn|
          @stream = conn
          conn.parent = self
        end
      end
      
    private
      
      def on_connect
        @stream.start_tls if @uri.scheme == 'wss'
        @handshake = @parser.create_handshake
        @message = []
        @stream.write(@handshake.request_data)
      end
      
      def receive_data(data)
        data = WebSocket.encode(data)
        
        case @ready_state
          when CONNECTING then
            @message += @handshake.parse(data)
            return unless @handshake.complete?
            
            if @handshake.valid?
              @ready_state = OPEN
              event = Event.new('open')
              event.init_event('open', false, false)
              dispatch_event(event)
              
              receive_data(@message)
            else
              @ready_state = CLOSED
              event = Event.new('error')
              event.init_event('error', false, false)
              dispatch_event(event)
            end
            
          when OPEN, CLOSING then
            @parser.parse(data)
        end
      end
      
      module Connection
        attr_accessor :parent
        
        def connection_completed
          parent.__send__(:on_connect)
        end
        
        def receive_data(data)
          parent.__send__(:receive_data, data)
        end
        
        def unbind
          parent.close(1006, '', false)
        end
        
        def write(data)
          send_data(data)
        end
      end
    end
    
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
faye-websocket-0.1.2 lib/faye/websocket/client.rb
faye-websocket-0.1.1 lib/faye/websocket/client.rb
faye-websocket-0.1.0 lib/faye/websocket/client.rb